Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/225.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Android中如何在字符串中插入新行_Android_String_Enter - Fatal编程技术网

在Android中如何在字符串中插入新行

在Android中如何在字符串中插入新行,android,string,enter,Android,String,Enter,这似乎是一个愚蠢的问题,但我一直在寻找,找不到答案 我正在创建一个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 email

这似乎是一个愚蠢的问题,但我一直在寻找,找不到答案

我正在创建一个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.
以下是我希望它做的事情:

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方法来实现这一点


谢谢。

尝试使用
System.getProperty(“line.separator”)
获取新行。

尝试:

String str = "my string \n my other string";
打印后,您将获得:

我的字符串


我的另一个字符串

我个人更喜欢使用“\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.";
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.";
输出

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.\nI am the second part.\n\nI am the third part.";
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()
方法为特定操作系统获取“
行.separator

但这根本不是必需的,因为这里的操作系统是固定的,也就是说,Android。因此,每次调用一个方法都是一个沉重且不必要的操作


此外,这还会增加代码长度,使其看起来有点凌乱A“\n”是甜蜜而简单的。

我在
CDATA
标记中使用

。 例如,my strings.xml文件包含如下项:

<item><![CDATA[<b>My name is John</b><br>Nice to meet you]]></item>

此外,我还学习了将\n放入字符串中可插入新的line@Jakar
System.getProperty(“line.separator”)
是独立于平台的,而
\n
不是。但是如果你正在开发android应用程序,这个平台是固定的,linux/android使用
\n
作为换行符@维希,说得好。而且
\r\n
更具跨系统兼容性(如果我错了,请纠正我),因此更适合传输数据。例如,如果您使用
\n
向发送电子邮件,并且该电子邮件在使用
\r\n
的系统上读取,则可能会丢失行数,而在大多数情况下,如果您将
\r\n
传递给只需要
\n
的系统,则会得到补偿。我可能错了,而且我没有消息来源。这正是我经过一段时间收集到的。字符串中的换行符后面有空格,但输出中没有显示,输出中还有一个额外的空行。@MatthewRead谢谢,我已经删除了空格。但我没有看到任何额外的空行。这不是独立于系统的。某些系统对行使用\r\nbreaks@Gibolt这是独立于系统的。Android仅在基于Linux的设备上运行,因此所有设备之间的\n一致。