英文:
Setting a new line from API call not creating newline
问题
设置新行而不进行网络调用对我有效:
mTextView.setText("Message 1 \nMessage 2");
[![enter image description here][1]][1]
但是当我从API调用中的JSON设置文本时,它对我不起作用:
mTextView.setText(userInfo.getDescription());
[![enter image description here][2]][2]
它在我的数据库中存储的格式是:hello \nTest message.
问题是什么?
编辑:
尝试这样做,但也不起作用
String x = description.replace("\n", System.lineSeparator());
Log.d(TAG, "formatDescription: Formatted: " + x);
return x;```
[![enter image description here][3]][3]
[1]: https://i.stack.imgur.com/5GDhz.png
[2]: https://i.stack.imgur.com/C2FSc.png
[3]: https://i.stack.imgur.com/F1Vcg.png
编辑2:
需要在``` .replace("\n", System.lineSeparator());``` 中添加额外的```"\"```才能使其起作用
<details>
<summary>英文:</summary>
Setting a new line without a network call works for me:
```mTextView.setText("Message 1 \nMessage 2");```
[![enter image description here][1]][1]
However when I set the text from the JSON in an API call it doesn't work for me:
```mTextView.setText(userInfo.getDescription());```
[![enter image description here][2]][2]
The format it's stored in my db is: ```hello \nTest message.```
What is the issue?
Edit:
Tried doing this, but doesn't work either
Log.d(TAG, "formatDescription: Description: " + description);
String x = description.replace("\n", System.lineSeparator());
Log.d(TAG, "formatDescription: Formatted: " + x);
return x;
[![enter image description here][3]][3]
[1]: https://i.stack.imgur.com/5GDhz.png
[2]: https://i.stack.imgur.com/C2FSc.png
[3]: https://i.stack.imgur.com/F1Vcg.png
Edit 2:
Need to add an extra ```"\"``` to ```.replace("\n", System.lineSeparator());``` for it to work
</details>
# 答案1
**得分**: 2
String description = userInfo.getDescription();
description = description.replace("\\n", System.lineSeparator());
System.out.println(description);
mTextView.setText(description);
<details>
<summary>英文:</summary>
String description = userInfo.getDescription();
description = description.replace("\\n", System.lineSeparator());
System.out.println(description);
mTextView.setText(description);
</details>
# 答案2
**得分**: 0
String desc = userInfo.getDescription().replaceAll("\\n", System.getProperty("line.separator"));
<details>
<summary>英文:</summary>
Try this:
String desc = userInfo.getDescription().replaceAll("\\n", System.getProperty("line.separator"));
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论