我需要从给定的String中删除多个子字符串。示例 -
String[] exclude = {"one","two","three"};
String input = "if we add one and two we get three"
我希望我的程序从输入字符串中删除所有出现的“一个”或“两个”或“三个”并返回 -
"if we add and we get"
我如何用Java做到这一点?
答案 0 :(得分:5)
虽然这个问题已经得到解答,但我对String替换性能感兴趣并做了一个小测试。因此,我只为所有对结果感兴趣的人添加我的示例代码。我已经用这种方式编写了测试,你也可以添加其他替换策略来测试你自己的测试。
我有一个测试驱动程序(没有JUnit可以更容易复制和粘贴)
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");
}
不同的字符串替换策略:
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;
}
}
我的电脑上的结果是:
StringReplaceAll took: 3456 ms for 1000000 iterations
StringReplace took: 3162 ms for 1000000 iterations
StringUtilsRemove took: 761 ms for 1000000 iterations
答案 1 :(得分:3)
你可以使用空字符串循环数组和replace输入中出现的每个String:
for(String str : exclude){
input = input.replace(str, "");
}
答案 2 :(得分:3)
如果没有StringUtils,您可以像这样实现它:
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 :(得分:2)
for(String s:exclude){
input=input.replace(s,"");
}