5

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 ?

2
  • 1
    Iterate over exclude and remove each string from input? Commented Aug 18, 2013 at 7:49
  • yeah thats what i thought too, i was looking for an alternate solution. Commented Aug 18, 2013 at 8:13

4 Answers 4

5

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)

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 the different string replace 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;
    }

}

The result on my computer is:

StringReplaceAll took: 3456 ms for 1000000 iterations
StringReplace took: 3162 ms for 1000000 iterations
StringUtilsRemove took: 761 ms for 1000000 iterations
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the info @Rene. Its amazing to see the difference between these approaches. I too was looking for the most efficient way to solve this problem. This answer provides all the info i need. Thanks !
3

You can loop on the array and replace each String that appears in the input from it with empty String:

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

Comments

3

Without StringUtils you could 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], "");
}

Comments

2
for(String s:exclude){
    input=input.replace(s,"");
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.