如何在Java中为String添加换行符?

时间:2016-03-31 10:37:49

标签: java html string line-breaks

在Java应用程序中,我正在创建一个如下所示的字符串(通过连接):

String notaCorrente = dataOdierna + " - " + testoNotaCorrente;

我的问题是我想在此String的末尾添加类似HTML换行符的内容(将显示在HTML页面中)。

我该如何实施?

3 个答案:

答案 0 :(得分:3)

Java中的换行符是“\ n”,如下所示:

String notaCorrente = dataOdierna + " - " + testoNotaCorrente + "\n";

但是,这不会像您期望的那样在HTML页面上显示。您可以尝试添加html break标记,或添加
(换行符)和
(回车)HTML实体:

String notaCorrente = dataOdierna + " - " + testoNotaCorrente + "<br>";

String notaCorrente = dataOdierna + " - " + testoNotaCorrente + "&#13;&#10";

答案 1 :(得分:1)

只需添加<br/> (break line tag of HTML)

String notaCorrente = dataOdierna + " - " + testoNotaCorrente + "<br/>";

因此,当您要显示此内容时,<br/> tag以新行的形式在HTML页面上呈现。

答案 2 :(得分:1)

对于将导致HTML换行的换行符,请使用

String notaCorrente = dataOdierna + " - " + testoNotaCorrente + "<br>";

对于将在文本编辑器中导致换行符的换行符,请使用

String notaCorrente = dataOdierna + " - " + testoNotaCorrente + System.lineSeparator();

对于两者,请使用

String notaCorrente = dataOdierna + " - " + testoNotaCorrente + "<br>" + System.lineSeparator();

为什么不\n

\n特定于某些操作系统,而其他操作系统则使用\r\nSystem.lineSeparator()将为您提供与您执行应用程序的系统相关的那个。有关此功能的详情,请参阅the documentation;有关新行的详细信息,请参阅Wikipedia

相关问题