2

I am trying to figure out how to write a method that will remove letters in a

string based on another string. The method would end up like so:

removeLetter("file", "fe")

The only thing that should be returned is the string "il". So far I have something like this:

public class h 
{
 public static void main(String[] args)
 {
   String a="file";
   String b="fe";
   char letter;
   int i;
   int j;
   for (letter = 'a'; letter <= 'z'; letter++)
   {
      for (i=0; i < a.length()-1; i++)
      {
          for (j=0; j < b.length()-1; j++) // This is the loop i get stuck on
          {     
              char r = b.charAt(j);
              char s = a.charAt(i);
              if ( letter == r && letter == s);
                  System.out.print(r + " " + s);        
          }
      } 

   }         
 }
} 

I know the bottom part is wrong but I am not sure where to go from here.

3
  • 1
    possible duplicate of Java String remove all non numeric characters
    – user201891
    Sep 23, 2015 at 21:39
  • Not an identical duplicate, but as can be seen in Andy's answer, the same principle applies. Also stackoverflow.com/questions/13386107/….
    – user201891
    Sep 23, 2015 at 21:40
  • The answer you accepted seems goofy to me, compared to the regex solution. I can only assume this is a homework assignment and you're not allowed to use regex, in which case you should have just asked for help with your code.
    – user201891
    Sep 24, 2015 at 19:10

2 Answers 2

4

You can do this with a regular expression:

a.replaceAll("[" + b + "]", "")

This works by constructing a character class like [fe], and replacing characters which match that with the empty string.

Of course, this is a bit of a hack, in that you can easily choose b such that it won't yield a valid regular expression. However, if you know that b will only ever contain letters, this would work.

3
  • I think you can use Pattern.escape to ensure the character class is always valid.
    – user201891
    Sep 23, 2015 at 21:28
  • Hm, not sure that will work if you insert it into a character class. For example, it might construct something like \Qfe\E, which wouldn't make sense if you were to wrap it in [], would it? Sep 23, 2015 at 21:29
  • You're right. I thought that escaping a character that doesn't need to be escaped in a character class would have no effect, but it appears to ignore the character altogether.
    – user201891
    Sep 23, 2015 at 21:33
-1

Here's a pretty simple nested array using a flag boolean :

public static void main(String[] args) {

    String a = "file";
    String b = "f";
    String c = "";
    StringBuilder sb = new StringBuilder();
    boolean contains;

    for (int i = 0 ; i < a.length() ; i++){
        contains = false;
        for (int j = 0 ; j < b.length() ; j++){
            if (a.charAt(i) == b.charAt(j)) contains = true;
        }
        if (!contains) sb.append(a.charAt(i));
    }

    System.out.println(sb);

}

It checks every char of the first word with the chars of the second and changes the flag to true if the char is contained in both.

If it is not the case, the char of the first word is added to the new String, if the contrary, nothing happens and we continue to the next char of the first String.

Let's remove all the vowels of this word : Supercalifragilisticexpialidocious

String a = "Supercalifragilisticexpialidocious";
String b = "aeiou";

Here's the output :

Sprclfrglstcxpldcs
3
  • You should (probably, almost) always use a StringBuilder to accumulate strings in a loop, to avoid the performance cost of repeatedly constructing strings. Sep 23, 2015 at 21:45
  • This went right along with what I have been learning and totally makes sense to me! Thank you so much!
    – john zoid
    Sep 23, 2015 at 21:50
  • @AndyTurner Thanks Andy, I did change that and I did learn something today too :) Sep 23, 2015 at 21:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.