I need to remove multiple substrings from a given String. Example -
String[] exclude = {"one","two","three"};
String input = "if we add one and two we get three"
I want my program to remove all occurrences of "one" or "two" or "three" from the input string and return -
"if we add and we get"
How can i do this in Java ?
Without StringUtils you could implement it like this:
Although the question is already answered I was interrested in String replace performance and made a small test. Thus I just add my example code for all who are also interrested in the result. I have written the test in this way that you can also add other replace strategies to test your own.
I have one test driver (no JUnit to make it easier for copy & paste)
And the different string replace strategies:
The result on my computer is:
You can loop on the array and replace each String that appears in the input from it with empty String: