Java Top

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

>> CHECK OUT THE COURSE

1. Overview

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.

2. Split String by Newline

2.1. Split String by Newline Using the System#lineSeparator Method

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.

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:

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

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.

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.

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[] 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

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.

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

In Java 8, Pattern class comes with a handy splitAsStream method.

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.

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.

2.5. Split String by Newline in Java 11

Java 11 makes splitting by newline really easy:

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.

As we can see, it'd be hard to find a simpler way to split a String by newline!

3. Conclusion

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.

As always, all these code samples are available over on GitHub.

Java bottom

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

>> CHECK OUT THE COURSE
Junit footer banner
4 Comments
Oldest
Newest
Inline Feedbacks
View all comments
Comments are closed on this article!