1. Overview
1.概述
In this tutorial, we’ll look at different ways to split a Java String by newline characters. Since the newline character is different in various operating systems, we’ll look at the method to cover Unix, Linux, Mac OS 9, and earlier, macOS, and Windows OS.
在本教程中,我们将研究通过换行字符拆分Java字符串的不同方法。由于换行符在不同的操作系统中是不同的,我们将看一下涵盖Unix、Linux、Mac OS 9及更早的版本、macOS和Windows操作系统的方法。
2. Split String by Newline
2.通过换行分割字符串
2.1. Split String by Newline Using the System#lineSeparator Method
2.1.使用System#lineSeparator方法通过换行来分割字符串
Given that the newline character is different in various operating systems, we can use system-defined constants or methods when we want our code to be platform-independent.
鉴于换行符在不同的操作系统中是不同的,当我们希望我们的代码与平台无关时,我们可以使用系统定义的常量或方法。
The System#lineSeparator method returns the line separator string for the underlying operating system. It returns the value of the system property line.separator.
System#lineSeparator方法返回底层操作系统的行分隔符字符串。它返回系统属性line.separator的值。
Therefore, we can use the line separator string returned by the System#lineSeparator method along with String#split method to split the Java String by newline:
因此,我们可以使用System#lineSeparator方法返回的分隔符字符串和String#split方法,通过换行来分割JavaString。
String[] lines = "Line1\r\nLine2\r\nLine3".split(System.lineSeparator());
The resulting lines will be:
由此产生的线条将是。
["Line1", "Line2", "Line3"]
2.2. Split String by Newline Using Regular Expressions
2.2.使用正则表达式通过换行来分割字符串
Next, let’s start by looking at the different characters used to separate lines in different operating systems.
接下来,让我们先看看不同操作系统中用来分隔行的不同字符。
The “\n” character separates lines in Unix, Linux, and macOS. On the other hand, the “\r\n” character separates lines in Windows Environment. Finally, the “\r” character separates lines in Mac OS 9 and earlier.
在Unix、Linux和macOS中,”\n“字符是分隔行的。另一方面,”\r\n“字符在Windows环境中分隔行。最后,”\r“字符在Mac OS 9和更早的版本中分隔行。
Therefore, we need to take care of all the possible newline characters while splitting a string by newlines using regular expressions.
因此,在使用正则表达式通过换行符分割字符串时,我们需要照顾到所有可能的换行符。
Finally, let’s look at the regular expression pattern that will cover all the different operating systems’ newline characters. That is to say, we need to look for “\n”, “\r\n” and “\r” patterns. This can be easily done by using regular expressions in Java.
最后,让我们来看看能涵盖所有不同操作系统换行字符的正则表达式模式。也就是说,我们需要寻找”\n”、”\r\n “和”\r “模式。这可以通过使用Java中的正则表达式轻松完成。
The regular expression pattern to cover all the different newline characters will be:
涵盖所有不同换行字符的正则表达式模式将是。
"\\r?\\n|\\r"
Breaking it down, we see that:
把它分解开来,我们看到。
- \\n = Unix, Linux and macOS pattern
- \\r\\n = Windows Environment pattern
- \\r = MacOS 9 and earlier pattern
Next, let’s use the String#split method to split the Java String. Let’s look at a few examples:
接下来,让我们使用String#split方法来分割JavaString。让我们看几个例子。
String[] lines = "Line1\nLine2\nLine3".split("\\r?\\n|\\r");
String[] lines = "Line1\rLine2\rLine3".split("\\r?\\n|\\r");
String[] lines = "Line1\r\nLine2\r\nLine3".split("\\r?\\n|\\r");
The resulting lines for all the examples will be:
所有例子的结果线将是。
["Line1", "Line2", "Line3"]
2.3. Split String by Newline in Java 8
2.3.在Java 8中通过换行分割字符串
Java 8 provides an “\R” pattern that matches any Unicode line-break sequence and covers all the newline characters for different operating systems. Therefore, we can use the “\R” pattern instead of “\\r?\\n|\\r” in Java 8 or higher.
Java 8提供了一个“\R”模式,可以匹配任何Unicode断行序列,并涵盖不同操作系统的所有换行字符。因此,在Java 8或更高版本中,我们可以使用“\R”模式而不是“\\r?\n|\r”。
Let’s look at a few examples:
让我们看看几个例子。
String[] lines = "Line1\nLine2\nLine3".split("\\R");
String[] lines = "Line1\rLine2\rLine3".split("\\R");
String[] lines = "Line1\r\nLine2\r\nLine3".split("\\R");
Again, the resulting output lines for all examples will be:
同样,所有例子的结果输出行将是。
["Line1", "Line2", "Line3"]
2.4. Split String by Newline Using Pattern Class
2.4.使用Pattern类用换行符分割字符串
In Java 8, Pattern class comes with a handy splitAsStream method.
在Java 8中,Pattern类带有一个方便的<a href=”https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/regex/Pattern.html#splitAsStream(java.lang.CharSequence)>splitAsStream方法。
In our case, we can utilize the “\R” pattern, but of course, this method can also be used to split String by any, more sophisticated, regular expression.
在我们的案例中,我们可以利用“\R”模式,当然,这个方法也可以用来通过任何更复杂的regular expression来分割String。
Let’s see it in action:
让我们看看它的行动。
Pattern pattern = Pattern.compile("\\R");
Stream<String> lines = pattern.splitAsStream("Line1\nLine2\nLine3");
Stream<String> lines = pattern.splitAsStream("Line1\rLine2\rLine3");
Stream<String> lines = pattern.splitAsStream("Line1\r\nLine2\r\nLine3");
As we can see, this time, instead of an array we get a Stream of Strings that we can easily process further.
我们可以看到,这次我们得到的不是一个数组,而是一个Stream的Strings,我们可以很容易地进一步处理。
2.5. Split String by Newline in Java 11
2.5.在Java 11中通过换行来分割字符串
Java 11 makes splitting by newline really easy:
Java 11使换行分割变得非常容易。
Stream<String> lines = "Line1\nLine2\rLine3\r\nLine4".lines();
Because lines() uses an “\R” pattern under the hood, it works with all kinds of line separators.
因为lines()在引擎盖下使用了“\R”模式,它可以与各种行的分隔符一起工作。
As we can see, it’d be hard to find a simpler way to split a String by newline!
正如我们所看到的,很难找到一种更简单的方法来通过换行来分割字符串!
3. Conclusion
3.总结
In this quick article, we looked at the different newline characters we’re likely to encounter in different operating systems. Furthermore, we saw how to split a Java String by newlines using our own regular expression pattern, as well as using the “\R” pattern available starting in Java 8.
在这篇快速文章中,我们看了在不同操作系统中可能遇到的不同换行符。此外,我们还看到了如何使用我们自己的正则表达式模式以及从Java 8开始使用的“\R”模式,通过换行符来分割一个Java字符串。
As always, all these code samples are available over on GitHub.
一如既往,所有这些代码样本都可以在GitHub上找到。