我需要從給定的字符串中刪除多個子字符串。示例 -從字符串中刪除多個子字符串 - Java
String[] exclude = {"one","two","three"};
String input = "if we add one and two we get three"
我想我的程序從輸入字符串中刪除「一」或「二」或「三」的所有出現,並返回 -
"if we add and we get"
我怎樣才能在Java中做到這一點?
我需要從給定的字符串中刪除多個子字符串。示例 -從字符串中刪除多個子字符串 - Java
String[] exclude = {"one","two","three"};
String input = "if we add one and two we get three"
我想我的程序從輸入字符串中刪除「一」或「二」或「三」的所有出現,並返回 -
"if we add and we get"
我怎樣才能在Java中做到這一點?
雖然這個問題已經得到解答,但我對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
感謝@Rene的信息。令人驚訝的是看到這些方法之間的差異。我也在尋找解決這個問題的最有效方法。這個答案提供了我需要的所有信息。謝謝 ! –
for(String s:exclude){
input=input.replace(s,"");
}
可以出現在輸入從中空字符串數組和replace每串上環:
for(String str : exclude){
input = input.replace(str, "");
}
沒有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], "");
}
迭代'exclude'並從'input'中移除每個字符串? – chrylis
是的,這就是我的想法,我正在尋找替代解決方案。 –