简体   繁体   中英

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.

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.

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 .

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. I need to format those line breaks to "\\n" so they don't break the JSON

在Java或Android中使用\\\\n转义\\中的\\n字符转义

尝试替换其他换行符: EditText.getText().replaceAll("\\r\\n|\\r", "\\n");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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