从字符串中删除多个子 - 的Java [英] 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"
如何在Java中做到这一点?
How can i do this in Java ?
推荐答案
虽然这个问题已经回答了我interrested在替换字符串的性能,并提出了小的考验。因此,我只需添加我的例子code为所有谁也interrested在结果中。我已经写了测试通过这种方式,您还可以添加其他替代战略,以测试自己的。
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.
我有一个测试车手(不JUnit来更容易复制和放大器;粘贴)
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
这篇关于从字符串中删除多个子 - 的Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!