Search code examples
javareplaceall

How can one use the Java replaceAll() method to using multiple strings in one method?


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!


Solution

  • As commented, you can do:

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

    Output:

    tt