Quantcast
Channel: Baeldung
Viewing all articles
Browse latest Browse all 4535

Split Java String by Newline

$
0
0

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

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.

The post Split Java String by Newline first appeared on Baeldung.
       

Viewing all articles
Browse latest Browse all 4535

Trending Articles