繁体   English   中英

无法使用 java 将 \\n 替换为空字符串

[英]Unable to replace \\n with empty string using java

Trying to replace \\n from a String with "",
after removing the \\n the expected o/p  would be:  "This is not working ";
Tried samples : 

 String str = "This \\nis \\n\\nnot \\n\\nworking ";

   str.replaceAll("\\n","");
  • 这是行不通的。
    你能帮我解决这个问题吗?

您必须使用\\\\n而不是\\n 这应该有效:

String str = "This \\nis \\n\\nnot \\n\\nworking ";
System.out.println(str.replaceAll("\\\\n",""));

您可以参考在线正则表达式生成器来探索更多模式。 这真的很有帮助!

同时我想下面的例子应该为你的用例服务:

public class RegularExpression {

public static void main(String[] args) {
    
    String originalString = "This \\nis \\n\\nnot \\n\\nworking ";

    String newString = originalString.replaceAll("\\\\n","");

    System.out.println(newString);

}

}

暂无
暂无

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

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