Java split String by new line example shows how to split string by new line in Java using regular expression as well as ignore empty lines. Example also covers files created in Windows, Unix and Mac OS.
How to split String by a new line in Java?
While processing a file or processing text area inputs you need to split string by new line characters to get each line. You can do this by using regular expressions in Java.
Different operating systems use different characters to represent a new line as given below.
1 2 3 |
Unix new line - \n Windows new line - \r\n Mac OS new line - \r (For newer version \n) |
While splitting a string by a new line, we need to take care of all the possible new line characters given above. So basically, we need to look for zero or one \r followed by \n or just \r. Converting it to regex pattern will become “\\r?\\n|\\r” where,
1 2 3 4 |
\\r? - zero or one \r \\n - followed by \n | - or \\r - only \r |
The above pattern will cover all three OS newline characters.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
package com.javacodeexamples.stringexamples; import java.util.Arrays; public class StringSplitByNewLineExample { public static void main(String[] args) { String strInput = "Line1\r\nLine2\nLine3\rLine4"; String[] strParts = strInput.split("\\r?\\n|\\r"); System.out.println("Total lines: " + strParts.length); //print lines System.out.println( Arrays.toString(strParts) ); } } |
Output
1 2 |
Total lines: 4 [Line1, Line2, Line3, Line4] |
How to include empty trailing lines?
The above pattern ignores the empty trailing line at the end of the string or file.
1 2 3 4 5 6 7 8 |
String strInput = "Line1\r\nLine2\nLine3\rLine4\n"; String[] strParts = strInput.split("\\r?\\n|\\r"); System.out.println("Total lines: " + strParts.length); //print lines System.out.println( Arrays.toString(strParts) ); |
Output
1 2 |
Total lines: 4 [Line1, Line2, Line3, Line4] |
The last empty line is not included in the output. You can include the trailing empty lines by using a limit parameter in the split
method as given below.
1 2 3 4 5 6 7 8 |
String strInput = "Line1\r\nLine2\nLine3\rLine4\n"; String[] strParts = strInput.split("\\r?\\n|\\r", -1); System.out.println("Total lines: " + strParts.length); //print lines System.out.println( Arrays.toString(strParts) ); |
Output
1 2 |
Total lines: 5 [Line1, Line2, Line3, Line4, ] |
How to ignore empty lines in between?
Sometimes string or file contains empty lines between the content as given below.
1 2 3 4 5 6 7 8 |
String strInput = "Line1\r\n\r\n\r\nLine2"; String[] strParts = strInput.split("\\r?\\n|\\r"); System.out.println("Total lines: " + strParts.length); //print lines System.out.println( Arrays.toString(strParts) ); |
Output
1 2 |
Total lines: 4 [Line1, , , Line2] |
There are two empty lines between Line1 and Line 2. If you want to ignore empty line between lines, you can use the "[\r?\n|\r]+"
regex pattern where,
1 2 3 4 5 6 |
[] - character group \r? - zero or one \r \n - followed by \n | - or \r - only \r []+ - group one or more times |
Our pattern will match one or more newlines since we applied “+” to the character group.
Note: We do not need to escape \r or \n because they are inside the character group ([]).
1 2 3 4 5 6 7 8 |
String strInput = "Line1\r\n\r\n\r\nLine2"; String[] strParts = strInput.split("[\r?\n|\r]+"); System.out.println("Total lines: " + strParts.length); //print lines System.out.println( Arrays.toString(strParts) ); |
Output
1 2 |
Total lines: 2 [Line1, Line2] |
How to split string by new line in Java 8?
If you are using Java 8, you can use "\R"
pattern instead of "\\r?\\n|\\r"
pattern. The "\R"
pattern covers all the new line characters.
1 2 3 4 5 6 7 8 |
String strInput = "Line1\r\nLine2\nLine3\rLine4\n"; String[] strParts = strInput.split("\\R"); System.out.println("Total lines: " + strParts.length); //print lines System.out.println( Arrays.toString(strParts) ); |
Output
1 2 |
Total lines: 4 [Line1, Line2, Line3, Line4] |
How to split string by new line using line.separator? (Not recommended)
The line.separator is a system property that gives default newline characters for the operating system. It can be used to split the string as given below.
1 2 3 4 5 6 7 8 9 |
String strInput = "Line1\r\nLine2"; String[] strParts = strInput.split( System.getProperty("line.separator") ); System.out.println("Total lines: " + strParts.length); //print lines System.out.println( Arrays.toString(strParts) ); |
Output
1 2 |
Total lines: 2 [Line1, Line2] |
Why it is not recommended? In the above example, the string contains \r\n as a new line and the output was generated in the Windows machine. So, when we get “line.separator” property, it returned Windows new line which is \r\n and program worked as expected.
If the same code was ran on Unix, “line.separator” would have returned \n and our code would have failed. This approach is not recommended for the same reason, it is platform dependent. Means you will not be able to process file generated in a different environment.
You can also split string by dot, pipe, and comma or you can split string in equal lengths as well.
This example is a part of Java String tutorial and Java RegEx tutorial.
Please let me know your views in the comments section below.