简体   繁体   English

如何制作multi replace()java

[英]How do I make multi replace() java

I have a string with \\r\\n , \\r , \\n or \\" characters in it. How can I replace them faster? 我有一个字符串,其中包含\\r\\n\\r \\n\\n\\"字符。如何更快地替换它们?

What I already have is: 我已经拥有的是:

String s = "Kerner\\r\\n kyky\\r hihi\\n \\\"";
System.out.println(s.replace("\\r\\n", "\n").replace("\\r", "").replace("\\n", "").replace("\\", ""));

But my code does not look beautiful enough. 但我的代码看起来不够漂亮。 I found on the Internet something like: 我在互联网上找到了类似的东西:

replace("\\r\\n|\\r|\\n|\\", "")

I tried that, but it didn't work. 我尝试过,但它没有用。

You can wrap it in a method, put /r/n , /n and /r in a list. 您可以将其包装在方法中,将/r/n/n/r放在列表中。 iterate the list and replace all such characters and return the modified string. 迭代列表并替换所有这些字符并返回修改后的字符串。

public String replaceMultipleSubstrings(String original, List<String> mylist){
    String tmp = original;
    for(String str: mylist){
        tmp = tmp.replace(str, "");
    }
    return tmp;
}

Test: 测试:

mylist.add("\\r");
mylist.add("\\r\\n");
mylist.add("\\n");
mylist.add("\\");  // add back slash

System.out.println("original:" + s);
String x = new Main().replaceMultipleSubstrings(s, mylist);
System.out.println("modified:" + x);

Output: 输出:

original:Kerner\r\n kyky\r hihi\n \"
modified:Kerner kyky hihi "

I don't know if your current replacement logic be correct, but it says now that either \\n , \\r , or \\r\\n gets replaced with empty string, and backslash also gets replaced with empty string. 我不知道你当前的替换逻辑是否正确,但它现在说, \\n\\r\\r\\n被替换为空字符串,反斜杠也被替换为空字符串。 If so, then you can try the following regex replace all: 如果是这样,那么你可以尝试以下正则表达式替换所有:

String s = "Kerner\\r\\n kyky\\r hihi\\n \\\"";
System.out.println(s.replaceAll("\\r|\\n|\\r\\n|\\\\", ""));

One problem I saw with your attempt is that you are calling replace() , not replaceAll() , so it would only do a single replacement and then stop. 我看到你尝试的一个问题是你正在调用replace() ,而不是replaceAll() ,所以它只会做一次替换然后停止。

String.replaceAll() can be used, in your question you tried to use String.replace() which does not interpret regular expressions, only plain replacement strings... 可以使用String.replaceAll() ,在您的问题中,您尝试使用String.replace() ,它不解释正则表达式,只使用普通替换字符串...

You also need to escape the \\\\ again, ie \\\\\\\\ instead of \\\\ 你还需要再次转义\\\\,即\\\\\\\\而不是\\\\

String s = "Kerner\\r\\n kyky\\r hihi\\n \\\"";
System.out.println(s.replaceAll("\\\\r|\\\\n|\\\\\"", ""));

Output 产量

Kerner kyky hihi

Note the differences between String.replaceAll() and String.replace() 注意String.replaceAll()和String.replace()之间的区别

String.replaceAll() String.replaceAll()

Replaces each substring of this string that matches the given regular expression with the given replacement. 将给定替换的给定正则 表达式匹配的此字符串的每个子字符串替换。

String.replace() 与string.replace()

Replaces each substring of this string that matches the literal target sequence with the specified literal replacement sequence . 将与该文字目标序列匹配的此字符串的每个子字符串替换为指定的文字替换序列

Use a regular expression if you want to do all the replaces in one go. 如果要一次性完成所有替换,请使用正则表达式。 http://www.javamex.com/tutorials/regular_expressions/search_replace.shtml http://www.javamex.com/tutorials/regular_expressions/search_replace.shtml

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM