[英]Reading a new line in a string with \n
When building a string, you can create newlines like so: 构建字符串时,可以像这样创建换行符:
"This is the first line \n\n And this is the second line";
So, when running this portion of code all works well on the Android Emulator: 因此,当运行这部分代码时,所有代码都可以在Android模拟器上正常运行:
TextView newsTextArea = (TextView) view.findViewById(R.id.newsTextView);
newsTextArea.setText("Hello \n\n Whats up");
However, I have downloaded and parsed JSON from a web service we have created, and I have stored what I want in a variable like so: 但是,我已经从创建的Web服务下载并解析了JSON,并且将所需的内容存储在这样的变量中:
GlobalSettings globalSettings = new GlobalSettings();
String newsText = globalSettings.getNews();
So the variable newsText
equals a string, which lets say for arguments sake here is "Hello, this has two lines. \\n\\n Welcome to the test"
. 因此变量newsText
等于一个字符串,为了便于说明,这里可以说是"Hello, this has two lines. \\n\\n Welcome to the test"
。
When I run the above TextView
code like this, it outputs it with the \\n\\n
as literal characters. 当我像这样运行上面的TextView
代码时,它以\\n\\n
作为文字字符输出。
newsTextArea.setText(newsText);
How can it be done so that the variable newsText
keeps the formatting? 如何使变量newsText
保持格式?
im a good guesser..lol well there may be soo many reasons that i think 我是一个很好的猜测者..大声笑我可能有太多原因
1. newsTextArea.setSingleLine(false);
2. newsTextArea.setMinLines(2); or newsTextArea.setMaxLines(50);//any figure
3. String newsText = globalSettings.getNews().replace("\\\n", System.getProperty("line.separator"));
4. String newsText = globalSettings.getNews().toString();
play with these methods and see if one works for ya or two lol 玩这些方法,看看有没有一个适合你,或者两个都笑了
However, I have downloaded and parsed JSON from a web service we have created ... 但是,我已经从我们创建的Web服务下载并解析了JSON ...
There are great chances that your JSON WS returns the literate sequence \\n
as its output. 您的JSON WS有很大的机会返回文化序列\\n
作为其输出。 Not the line-feed character ( <LF>
) as you expected it. 不是您期望的换行字符 ( <LF>
)。
At this point you probably have two options: 此时,您可能有两个选择:
\\
will pop out in your data). 就我自己而言,我不会朝着这个方向努力,因为它将很快成为维护的噩梦(合法的\\
会突然出现在您的数据中)。 Maybe it is time to post an other question, this time about your JSON web service ? 也许是时候发布另一个有关您的JSON Web服务的问题了吗?
It is likely that wherever that text comes from in the first place, the backslash is getting escaped. 无论文本最初来自何处,反斜杠都有可能被转义。 For example, it may be coming from a hard-coded constant in your service, entered into a form by a human and then validated, or etc. 例如,它可能来自您服务中的硬编码常量,然后由人工输入到表单中然后经过验证等。
Look at your JSON in its raw format before it is parsed. 在解析之前,请以原始格式查看JSON。 The JSON spec says that a single backslash followed by n means the newline character. JSON规范说,单个反斜杠后跟n表示换行符。 If it looks like ["Hello, this has two lines. \\n\\n Welcome to the test"]
then you should be good, because the JSON parser should interpret the newline characters correctly. 如果看起来像["Hello, this has two lines. \\n\\n Welcome to the test"]
那么您应该很好,因为JSON解析器应该正确解释换行符。 However, if it looks like this: ["Hello, this has two lines. \\\\n\\\\n Welcome to the test"]
, then the backslash character is being escaped, not the n. 但是,如果看起来像这样: ["Hello, this has two lines. \\\\n\\\\n Welcome to the test"]
,则转义了反斜杠字符,而不是n。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.