Java replaceAll("\\s+") vs replaceAll("\\\\s+")

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27172649/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 03:55:54  来源:igfitidea点击:

Java replaceAll("\\s+") vs replaceAll("\\\\s+")

javareplaceall

提问by Maxim Gotovchits

What's the difference between replaceAll("\\s+")and replaceAll("\\\\s+")? Usually I use \\s+but sometimes I see \\\\s+.

replaceAll("\\s+")和 和有replaceAll("\\\\s+")什么区别?通常我使用,\\s+但有时我会看到\\\\s+.

采纳答案by TheLostMind

\\s+--> replaces 1 or more spaces.

\\s+--> 替换 1 个或多个空格。

\\\\s+--> replaces the literal \followed by s one or more times.

\\\\s+-->\一次或多次替换后跟 s的文字。

Code:

代码:

public static void main(String[] args) {
    String s = "\sbas  def";
    System.out.println(s);
    System.out.println(s.replaceAll("\s+", ""));
    System.out.println(s.replaceAll("\\s+", ""));

}

O/P :

开/关:

\sbas  def
\sbasdef
 bas  def