1. Overview
1.概述
String formatting and generating text output often comes up during programming. In many cases, there is a need to add a new line to a string to format the output.
在编程过程中经常会出现字符串格式化和生成文本输出。在许多情况下,需要在字符串中添加一个新的行来格式化输出。
Let’s discuss how to use newline characters.
我们来讨论一下如何使用换行符。
2. Adding Newline Characters in a String
2.在一个字符串中添加换行符
Operating systems have special characters denoting the start of a new line. For example, in Linux a new line is denoted by “\n”, also called a Line Feed. In Windows, a new line is denoted using “\r\n”, sometimes called a Carriage Return and Line Feed, or CRLF.
操作系统有一些特殊的字符来表示新行的开始。例如,在Linux中,新的一行由”\n”表示,也称为Line Feed。在Windows中,新的一行是用”\r\n”表示的,有时被称为回车和换行,或CRLF。
Adding a new line in Java is as simple as including “\n” , “\r”, or “\r\n” at the end of our string.
在Java中添加一个新行,就像在我们的字符串末尾加入”\n”、”\r”或”\r\n “那么简单。在我们的字符串的末尾。
2.1. Using CRLF Line-Breaks
2.1.使用CRLF换行符
For this example, we want to create a paragraph using two lines of text. Specifically, we want line2 to appear in a new line after line1.
在这个例子中,我们想用两行文字创建一个段落。具体来说,我们希望line2出现在line1之后的一个新行。
For a Unix/Linux/New Mac-based OS we can use “\n”:
对于基于Unix/Linux/New Mac的操作系统,我们可以使用”\n”:。
String line1 = "Humpty Dumpty sat on a wall.";
String line2 = "Humpty Dumpty had a great fall.";
String rhyme = line1 + "\n" + line2;
If we are on a Windows-based OS, we can use “\r\n”:
如果我们是在一个基于Windows的操作系统上,我们可以使用”
rhyme = line1 + "\r\n" + line2;
For an old Mac-based OS, we can use “\r”:
对于基于Mac的旧操作系统,我们可以使用”\r”:。
rhyme = line1 + "\r" + line2;
We’ve demonstrated three methods of adding a new line, but unfortunately, they’re platform dependent.
我们已经演示了三种添加新行的方法,但不幸的是,它们都与平台有关。
2.2. Using Platform Independent Line Separators
2.2.使用独立于平台的行分隔符
We can use system defined constants when we want our code to be platform independent.
当我们希望我们的代码独立于平台时,我们可以使用系统定义的常量。
For example, using System.lineSeparator() for giving a line separator:
例如,使用System.lineSeparator()来给出一个行的分隔符:
rhyme = line1 + System.lineSeparator() + line2;
Or we could also use System.getProperty(“line.separator”):
或者我们也可以使用System.getProperty(“line.separator”):
rhyme = line1 + System.getProperty("line.separator") + line2;
2.3. Using Platform Independent Newline Characters
2.3.使用独立于平台的换行字符
Although line separators provide platform independence, they force us to concatenate our strings.
虽然行分隔符提供了平台独立性,但它们迫使我们将字符串串联起来。
If we are using something like System.out.printf or String.format, then the platform independent newline character, %n, can be used directly within a string:
如果我们使用System.out.printf或String.format这样的东西,那么平台独立的换行符,%n,可以直接在一个字符串内使用。
rhyme = "Humpty Dumpty sat on a wall.%nHumpty Dumpty had a great fall.";
This is the same as including System.lineSeparator() within our string, but we don’t need to divide the string into multiple parts.
这与在我们的字符串中包含System.lineSeparator()是一样的,但我们不需要将字符串分成多个部分。
3. Adding Newline Characters in an HTML Page
3.在HTML页面中添加换行字符
Suppose we are creating a string that is part of an HTML page. In that case, we can add an HTML break tag <br>.
假设我们正在创建一个字符串,它是一个HTML页面的一部分。在这种情况下,我们可以添加一个HTML中断标签<br>。
We can also use Unicode characters “& #13;” (Carriage Return) and “& #10;” (Line Feed). Although these characters work, they don’t work exactly like we might expect them to across all platforms. Instead, it is better to use <br> for line breaks.
我们还可以使用Unicode字符“& #13;” (回车)和“& #10;” (换行)。尽管这些字符可以工作,但它们并不完全像我们期望的那样在所有平台上工作。相反,最好使用<br>来换行。
Additionally, we can use “\n” in some HTML elements to break a line.
此外,我们可以在一些HTML元素中使用“n”来断开一行。
Overall, these are the three methods of breaking a line in HTML. We can decide which one to use depending on the HTML tag we are using.
总的来说,这就是HTML中断行的三种方法。我们可以根据我们所使用的HTML标签来决定使用哪一种。
3.1. HTML Break Tag
3.1. HTML断裂标签
We can use HTML break tag <br> to break a line:
我们可以使用HTML中断标签<br>来中断一个行。
rhyme = line1 + "<br>" + line2;
The <br> tag for breaking a line works in almost all HTML elements like <body>, <p>, <pre>, etc. However, note that it does not work in the <textarea> tag.
用于断行的<br>标签在几乎所有的HTML元素中都有效,如<body>、<p>、<pre>、等。然而,请注意,它在<textarea>标签中不起作用。
3.2. Newline Character
3.2.换行符
We can use ‘\n’ to break a line if text is enclosed in <pre> or <textarea> tag:
如果文本被包围在<pre>或<textarea>标签中,我们可以使用‘n’来断开一行。
rhyme = line1 + "\n" + line2;
3.3. Unicode Characters
3.3.Unicode字符
Finally, we can use Unicode characters “& #13;” (Carriage Return) and “& #10;” (Line Feed) to break a line. For example, in the <textarea> tag we can use either of these:
最后,我们可以使用Unicode字符“& #13;” (回车)和“& #10;” (换行)来断开一个行。例如,在<textarea>标签中,我们可以使用其中任何一个。
rhyme = line1 + "
" + line2;
rhyme = line1 + "
" + line2;
For the <pre> tag, both of the lines below will work:
对于<pre>标签,下面的两行都可以工作。
rhyme = line1 + "
" + line2;
rhyme = line1 + "
" + line2;
4. The Difference Between \n and \r
4.The Difference Between ñ and \r(区别)
\r and \n are characters denoted with ASCII values of 13 (CR) and 10 (LF), respectively. They both represent a break between two lines, but operating systems use them differently.
和/n是分别用ASCII值13(CR)和10(LF)表示的字符。它们 都代表两行之间的断开,但操作系统对它们的使用有所不同。
On Windows, a sequence of two characters is used to start a new line, CR immediately followed by LF. Conversely, on Unix-like systems, only LF is used.
在Windows系统中,使用两个字符的序列来开始一个新的行,即CR紧接着LF。相反,在类似Unix的系统上,只使用LF。
When writing Java applications, we must pay attention to the line break characters we use because the applications will behave differently depending on the operating system they will run on.
在编写Java应用程序时,我们必须注意我们所使用的断行字符,因为应用程序将根据它们将运行的操作系统而表现出不同的效果。
The safest and most cross-compatible option is to use System.lineSeparator(). This way, we won’t have to take the operating system into account.
最安全、最具有交叉兼容性的选择是使用System.lineSeparator()。这样一来,我们就不必考虑操作系统的问题了。
5. Conclusion
5.总结
In this article, we discussed how to add newline characters to a string in Java.
在这篇文章中,我们讨论了如何在Java中向字符串添加换行符。
We also saw how to write platform independent code for a new line using System.lineSeparator() and System.getProperty(“line.separator”).
我们还看到了如何使用System.lineSeparator()和System.getProperty(“line.separator”)为一个新行编写独立于平台的代码。
Finally, we wrapped up with how to add a new line in case we are generating an HTML page.
最后,我们总结了如何在生成HTML页面的情况下添加新行。
The full implementation of this article can be found over on GitHub.
本文的完整实现可以在GitHub上找到over。