2017-12-18 1045 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"));