我想用“\n”替換換行符,但我的代碼不起作用。str.replaceAll("\n","\n"));預期:“Hello\n World”實際的:“你好世界”
3 回答

隔江千里
TA貢獻1906條經驗 獲得超10個贊
嘗試這個。
class Scratch {
public static void main(String[] args) {
System.out.println("Hello\n world".replace("\n", "\\n"));
}
}

SMILET
TA貢獻1796條經驗 獲得超4個贊
您可以使用以下 3 種方法來使您的代碼正常工作:
#1
text = str.replace("\n", "\\n");
#2
text = str.replace(System.getProperty("line.separator"), "\\n");
#3
text = text.replaceAll("\\n", "\\\\n");
添加回答
舉報
0/150
提交
取消