-1

I was wondering if it was possible to use the replaceAll() String method in Java with multiple Strings to search. In other words, is it possible to have multiple Strings get searched in one replaceAll() method? I tried this:

foo.replaceAll("a" || "b", "");

However I got an error... And I'm pretty sure I can only use || in an if statement sooo I don't know why I was thinking it would work. Help is appreciated!

vPrototype
  • 109
  • 1
  • 8

2 Answers2

0

It will work correctly just change your code like that:

"abc".replaceAll("a|b", "");

Method replaceAll receives the one String regexp parameter, so you should use "a|b" not "a" || "b"

Dmitrii B
  • 2,672
  • 3
  • 5
  • 14
0

As commented, you can do:

String str = "atbbbt";
String newStr = str.replaceAll("a|b", "");

System.out.println(newStr);

Output:

tt
Most Noble Rabbit
  • 2,728
  • 2
  • 6
  • 12