简体   繁体   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","");
  • This is not working.这是行不通的。
    Could you please Help me out how to fix this issue?你能帮我解决这个问题吗?

You'll have to use \\\\n instead of a \\n .您必须使用\\\\n而不是\\n This should work:这应该有效:

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

You can refer to online regex generator to explore more patterns.您可以参考在线正则表达式生成器来探索更多模式。 This would really help!这真的很有帮助!

Meanwhile I guess below example should serve your use case:同时我想下面的例子应该为你的用例服务:

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