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.

john zoid
  • 31
  • 2
  • 1
    possible duplicate of [Java String remove all non numeric characters](http://stackoverflow.com/questions/10372862/java-string-remove-all-non-numeric-characters) – DavidS Sep 23 '15 at 21:39
  • Not an identical duplicate, but as can be seen in Andy's answer, the same principle applies. Also https://stackoverflow.com/questions/13386107/java-how-to-remove-single-character-from-a-string. – DavidS Sep 23 '15 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. – DavidS Sep 24 '15 at 19:10

2 Answers2

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.

Andy Turner
  • 137,514
  • 11
  • 162
  • 243
  • I think you can use [`Pattern.escape`](https://docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.html#quote-java.lang.String-) to ensure the character class is always valid. – DavidS Sep 23 '15 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? – Andy Turner Sep 23 '15 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. – DavidS Sep 23 '15 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
Yassin Hajaj
  • 21,337
  • 9
  • 51
  • 89
  • You should (probably, almost) always use a `StringBuilder` to accumulate strings in a loop, to avoid the performance cost of repeatedly constructing strings. – Andy Turner Sep 23 '15 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 '15 at 21:50
  • @AndyTurner Thanks Andy, I did change that and I did learn something today too :) – Yassin Hajaj Sep 23 '15 at 21:51