这可能看起来像是一个愚蠢的问题,但我一直在寻找并且无法找到答案。
我正在创建一个Android应用程序,其中有一个按钮,可以在电子邮件中发送一些信息,而且我不想把所有内容都放在一个段落中。
以下是我的应用为电子邮件正文的putExtra所做的工作:
I am the first part of the info being emailed. I am the second part. I am the third part.
这就是我想要它做的事情:
I am the first part of the info being emailed.
I am the second part.
I am the third part.
如何将新行放入字符串或使用putExtra方法来实现?
谢谢。
答案 0 :(得分:76)
尝试:
String str = "my string \n my other string";
打印时,您将获得:
my string
my other string
答案 1 :(得分:71)
尝试使用System.getProperty("line.separator")
获取新行。
答案 2 :(得分:18)
我个人更喜欢使用“ \ n ”。这只是在Linux或Android中放置一个换行符。
例如,
String str = "I am the first part of the info being emailed.\nI am the second part.\n\nI am the third part.";
<强>输出强>
I am the first part of the info being emailed.
I am the second part.
I am the third part.
更通用的方法是使用,
System.getProperty("line.separator")
例如,
String str = "I am the first part of the info being emailed." + System.getProperty("line.separator") + "I am the second part." + System.getProperty("line.separator") + System.getProperty("line.separator") + "I am the third part.";
带来与上面相同的输出。这里,getProperty()
类的静态System
方法可用于获取特定操作系统的“line.seperator
”。
但这根本不是必需的,因为此处的操作系统是固定的,即 Android 。因此,每次调用方法都是繁重且不必要的操作。
此外,这也会增加您的代码长度并使其看起来有点混乱。 “\ n”是甜蜜而简单的。
答案 3 :(得分:2)
我在<br>
标记中使用CDATA
。
例如,我的strings.xml文件包含如下项目:
<item><![CDATA[<b>My name is John</b><br>Nice to meet you]]></item>
并打印
My name is John
Nice to meet you
答案 4 :(得分:0)
如果您想在运行时将换行符添加到您正在推导值的同一字符串中的字符串中,那么此 Kotlin 代码对我有用:
str = "<br>"+str?.replace("," , "</br><br>")+"</br>"
value = HtmlCompat.fromHtml(${skill_et_1}",Html.FROM_HTML_MODE_LEGACY)
tv.text = value