簡體   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