-2
myString = "my lucky numbers are 6, 8, and 19."

I'm calling myString.remove("ra6"), and this should return "my lucky numbes e 6, 8, nd 19".

Here's what I'm trying to do.

public String remove(String arg) {
    if (myString != null) {
        char[] arrayOfChars1 = myString.toCharArray();
        char[] arrayOfChars2 = arg.toCharArray();
        char[] newArray = 
    }
    return null;
}

I'm trying to create an arrayOfChars1 so that I can compare it to the given String which would be another arrayOfChars2.

and a third array to store the comparison. If a char is in both array (arrayOfChar1 and arrayOfChar2) is found it should be removed but only the alphabets not numbers.

myString = "my lucky numbers are 6, 8, and 19."
myString.remove("ra6") //6 should be ignored.

return "my lucky numbe(r)s (a)(r)e 6, 8, and 19."

only char 'a' and 'r' should be removed

Unmitigated
  • 76,500
  • 11
  • 62
  • 80
Jethro
  • 31
  • 5

1 Answers1

2

You can first remove all non letter characters from the String of characters to replace and use String#replaceAll with a character class.

String s = "my lucky numbers are 6, 8, and 19.";
String remove = "ra6";
String res = s.replaceAll("["+
      remove.replaceAll("[^A-Za-z]", "")
     +"]", "");
System.out.println(res);

only char 'a' and 'r' should be removed

You can replace the regex, [ar] with "". The regex, [ar] means one of the characters inside the square bracket and is known as Character Classes.

String myString = "my lucky numbers are 6, 8, and 19.";
myString = myString.replaceAll("[ar]", "");
System.out.println(myString);// my lucky numbes e 6, 8, nd 19.
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
Unmitigated
  • 76,500
  • 11
  • 62
  • 80