从字符串中删除多个子字符串 - Java

Posted

技术标签:

【中文标题】从字符串中删除多个子字符串 - Java【英文标题】:Remove multiple substrings from a string - Java 【发布时间】:2013-08-20 05:59:33 【问题描述】:

我需要从给定的字符串中删除多个子字符串。示例 -

String[] exclude = "one","two","three";
String input = "if we add one and two we get three"

我希望我的程序从输入字符串中删除所有出现的“一”或“二”或“三”并返回 -

"if we add and we get"

我如何在 Java 中做到这一点?

【问题讨论】:

遍历exclude 并从input 中删除每个字符串? 是的,我也是这么想的,我正在寻找替代解决方案。 【参考方案1】:

虽然问题已经得到解答,但我对字符串替换性能很感兴趣并做了一个小测试。因此,我只是为所有对结果感兴趣的人添加了我的示例代码。我以这种方式编写了测试,您还可以添加其他替换策略来测试您自己的。

我有一个测试驱动程序(没有 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 提供的信息。看到这些方法之间的差异令人惊讶。我也在寻找解决这个问题的最有效方法。这个答案提供了我需要的所有信息。谢谢!【参考方案2】:

您可以在数组上循环,replace 每个出现在输入中的字符串都带有空字符串:

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

【讨论】:

【参考方案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], "");

【讨论】:

【参考方案4】:
for(String s:exclude)
    input=input.replace(s,"");

【讨论】:

以上是关于从字符串中删除多个子字符串 - Java的主要内容,如果未能解决你的问题,请参考以下文章

1.java编程将从键盘输入文本中的子字符串“word”替换为字符串“world”,并删除所有的子字符串“this”

使用正则表达式从python中的列表项中删除子字符串

如何在编译时从 string_view 中删除子字符串?

《Java》期末课程设计

Hive - 从字符串中删除子字符串

从字符串中删除子字符串