Remove multiple substrings from a string - Java

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?

+4
source share
4 answers

Despite the fact that the question has already been answered, I was tried in String, replaced the performance and did a little test. Thus, I simply add my sample code to everyone who is also interested in the result. I wrote the test in such a way that you can also add other replacement strategies to test your own.

I have one test driver (no JUnit to make copying and pasting easier)

 public class StringReplaceTest { public static void main(String[] args) { int iterations = 1000000; String[] exclude = { "one", "two", "three" }; String input = "if we add one and two we get three"; StringRemove replaceAll = new StringReplaceAll(); StringRemove replace = new StringReplace(); StringRemove stringUtilsRemove = new StringUtilsRemove(); // check if the replacement is implemented correctly assertStringRemove(replaceAll); assertStringRemove(replace); assertStringRemove(stringUtilsRemove); profileStringRemove(replaceAll, input, exclude, iterations); profileStringRemove(replace, input, exclude, iterations); profileStringRemove(stringUtilsRemove, input, exclude, iterations); } private static void assertStringRemove(StringRemove stringRemove) { String[] exclude = { "one", "two", "three" }; String input = "if we add one and two we get three"; String replaced = stringRemove.remove(input, exclude); String expected = "if we add and we get "; if (!expected.equals(replaced)) { throw new IllegalStateException( "String was not replaced correctly. Excpected <" + expected + "> but was <" + replaced + ">"); } } private static void profileStringRemove(StringRemove stringRemove, String input, String[] subStringsToRemove, int iterations) { long start = System.currentTimeMillis(); int testCount = iterations; while (iterations-- > 0) { stringRemove.remove(input, subStringsToRemove); } long end = System.currentTimeMillis(); printSummery(stringRemove.getClass().getSimpleName(), testCount, start, end); } private static void printSummery(String action, int iterations, long start, long end) { System.out.println(action + " took: " + (end - start) + " ms for " + iterations + " iterations"); } 

And various string replacement strategies:

 public interface StringRemove { public String remove(String input, String... subStringsToRemove); } public class StringReplaceAll implements StringRemove { public String remove(String input, String... subStringsToRemove) { for (int ix = 0; ix < subStringsToRemove.length; ix++) { input = input.replaceAll(subStringsToRemove[ix], ""); } return input; } } public class StringReplace implements StringRemove { public String remove(String input, String... subStringsToRemove) { for (int ix = 0; ix < subStringsToRemove.length; ix++) { int replaceLength = 0; while (replaceLength != input.length()) { input = input.replace(subStringsToRemove[ix], ""); replaceLength = input.length(); } } return input; } } public class StringUtilsRemove implements StringRemove { public String remove(String input, String... subStringsToRemove) { for (int ix = 0; ix < subStringsToRemove.length; ix++) { input = StringUtils.remove(input, subStringsToRemove[ix]); } return input; } } 

Result on my computer:

 StringReplaceAll took: 3456 ms for 1000000 iterations StringReplace took: 3162 ms for 1000000 iterations StringUtilsRemove took: 761 ms for 1000000 iterations 
+5
source

You can loop over the array and replace each row that appears at the input from it with an empty row:

 for(String str : exclude){ input = input.replace(str, ""); } 
+3
source

Without StringUtils, you can implement it like this:

 String[] exclude = {"one","two","three"}; String input = "if we add one and two we get three"; for (int ix = 0; ix < exclude.length; ix++) { input.replaceAll(exclude[ix], ""); } 
+3
source
 for(String s:exclude){ input=input.replace(s,""); } 
+2
source

Source: https://habr.com/ru/post/1497645/