2017-12-18 1051 views
-3

我一直想打印\ n的輸出通過更換\r\nJava:如何在輸出中打印\ n?

我寫我的代碼,例如:"John \r\n find".replaceAll("\r\n", "\\\\n"));

但產量之際,這樣的總是:

John \n find 

我想要的輸出爲:John \\n find

+0

不是4只有2 \將不夠用 –

+1

使用「John \ r \ n find」.replaceAll(「\ r \ n」,「\\\\\\\\ n」));正如@Kayaman所建議的那樣 –

回答

0

A 平臺無關替換字符串的方法\r\n用新的在線分離器將是:

String s = "John \r\n find".replaceAll("\r\n", System.getProperty("line.separator")); 
System.out.println(s); 

輸出是(注意的是,坯件被保持,當然):

John 
find 

爲了替換空白字符以及使用:

"John \r\n find".replaceAll(" \r\n ", System.getProperty("line.separator"));