In this post, we will see how to split String by newline in java.
Splitting a string by a newline character is one of the most common string operations performed in java, broadly there are two ways of achieving this task as mentioned below.
Table of Contents
While considering new line characters, it is important to understand that the character differs based on the operating system, and the regular expression must be formed in such a way that it covers new line characters for all the operating systems, so that the code is not platform dependent.
Let’s now take a look at different operating systems and their corresponding new line characters.
Operating System
|
New line character
|
---|---|
Windows
|
\r\n
|
Linux
|
\n |
macOS
|
Older version \r Newer versions \n |
Next, let’s start looking in detail at each method mentioned earlier in detail.
Split by new line character using regular expressions and split() method
As already discussed splitting the string by newline required us to consider the newline characters of the different operating systems so as to provide a generic platform-independent solution for the problem, This requires us to form a regex that will be zero or 1 combination of \r followed by \r or just \r based on the operating system we are considering.
Taking the above into consideration out regex will be “\r?\n|\r”.
Next, let’s try to understand this regex by understanding each of its parts.
- \r? Zero or one \r
- \n one \n
- \r one \r
Note, here we are using \\
as single \
is an escape character.
Let’s see a code example for this.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
package org.arpit.java2blog; import java.util.Arrays; public class StringSplitterByNewline{ public static void main(String args[]){ String input = "This is a simple String\r\n\n\nLet’s try to split this\nOne more line"; String[] linesArr = input.split("\\r?\\n|\\r"); System.out.println(Arrays.toString(linesArr)); } } |
Output:
If you don’t want empty space in between the line, you can use [\\r?\\n|\\r]
as regex.
Here is an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
package org.arpit.java2blog; import java.util.Arrays; public class StringSplitterByNewline{ public static void main(String args[]){ String input = "This is a simple String\r\n\n\nLet’s try to split this\nOne more line"; String[] linesArr = input.split("[\\r?\\n|\\r]+"); System.out.println(Arrays.toString(linesArr)); } } |
Using line.separator
line.separator
is a property of System class in java that provides default newline characters for the operating system let’s understand with an example below.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
package org.arpit.java2blog; import java.util.Arrays; public class StringSplitterByNewline{ public static void main(String args[]){ String input = "First Line\r\nSecond Line"; String[] linesArr = input.split(System.getProperty("line.separator")); System.out.println(Arrays.toString(linesArr)); } } |
Output:
Although, this is one of the approaches to split the string it is not recommended as it is platform dependent.
Split by new line character using regular expressions and split() method using java 8
Java 8 provides a simplified way of splitting a string by reducing the complexity of regular expression, so instead of using the regex \\r?\\n|\\r
we can just make use of \R
and it takes care of all the new line characters.
Note: the fact that R is of capital case, java 8 is able to achieve this.
Let’s see it in action below.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
package org.arpit.java2blog; import java.util.Arrays; public class StringSplitterByNewline{ public static void main(String args[]){ String input = "First Line\r\nSecond Line"; String[] linesArr = input.split("\\R"); System.out.println(Arrays.toString(linesArr)); } } |
Output:
Using String’s lines() method [java 11]
Java 11 has introduced new method lines()
in String class. It returns stream of lines extracted from the string, splitted by line terminators.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
package org.arpit.java2blog; import java.util.Arrays; public class StringSplitterByNewline{ public static void main(String args[]){ String inputStr = "First Line\r\nSecond Line\rThird Line"; String[] linesArr = inputStr.lines().toArray(String[]::new); System.out.println(Arrays.toString(linesArr)); } } |
Output:
Since this method returns a stream, so it opens up lot of options to enable you writing concise and declarative style code.
Using guava library
You can use guava library Splitter class with multiple options such as trimResults()
and omitEmptyStrings()
to split string by new line in java.
Here is the dependency which you need to add for guava in pom.xml
.
1 2 3 4 5 6 7 |
<dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>29.0-jre</version> </dependency> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
package org.arpit.java2blog; import com.google.common.base.Splitter; import java.util.Arrays; public class StringSplitterByNewline{ public static void main(String args[]){ String inputStr = "First Line\r\nSecond Line\n\n\rThird Line"; Iterable<String> splitIter = Splitter.onPattern("\\r?\\n|\\r") .trimResults() .omitEmptyStrings() .split(inputStr); System.out.println(splitIter); } } |
Output:
That’s all about how to split String by newline in java.