[英]Replace new line with n in Java
I'm using Android Studio and I'm trying to pass a multi-line EditText
field to a JSON
string. 我正在使用Android Studio,并且试图将多行
EditText
字段传递给JSON
字符串。
The problem I'm having is that line breaks within the EditText
aren't being formatted as \\n
when being passed to the JSON
string and the JSON
then breaks. 我遇到的问题是,传递给
JSON
字符串时, EditText
中的换行符没有被格式化为\\n
,然后JSON
中断了。
I can't figure out how to replace an actual line break, with \\n
from the EditText.getText()
so I can pass it correctly to the JSON
. 我无法弄清楚如何用
EditText.getText()
\\n
替换实际的换行符,因此可以将其正确传递给JSON
。
To add some extra clarity.. The user types in: 为了增加一些清晰度。用户输入:
"This is a note. “这是一张纸条。
This is a few lines down" 这几行下来
This is being passed exactly as above to the JSON
string. 完全如上所述将其传递给
JSON
字符串。 I need to format those line breaks to "\\n" so they don't break the JSON
我需要将这些换行符的格式设置为“ \\ n”,以便它们不会破坏
JSON
在Java或Android中使用\\\\n
转义\\
中的\\n
字符转义
尝试替换其他换行符: EditText.getText().replaceAll("\\r\\n|\\r", "\\n");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.