简体   繁体   English

在Java中用n替换新行

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM