Java split String by new line Example

April 10, 2016

1,150 Views

4 Min Read

Java split String by new line example shows how to split string by new line in Java. Example also shows how to split string by new line using regular expression and ignore empty lines. Example also covers files created in Windows, Unix and Mac OS.

How to split String by 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 expression 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 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

Above pattern will cover all three OS new line characters. Below given is the example of split string by new line.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

packagecom.javacodeexamples.stringexamples;

importjava.util.Arrays;

publicclassStringSplitByNewLineExample{

publicstaticvoidmain(String[]args){

StringstrInput="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?

Above pattern ignores the empty trailing line at the end of the string or file.

1

2

3

4

5

6

7

8

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

Last empty line is not included in the output. You can include the trailing empty lines by using limit parameter in split method as given below.

1

2

3

4

5

6

7

8

StringstrInput="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

StringstrInput="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 “[\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 new lines since we applied “+” to the character group.

Note: We do not need to escape \r or \n because they are inside character group ([]).

1

2

3

4

5

6

7

8

StringstrInput="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. “\R” covers all the new line characters.

1

2

3

4

5

6

7

8

StringstrInput="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)

line.separator is a system property which gives default new line characters for the operating system. It can be used to split string as given below.

1

2

3

4

5

6

7

8

9

StringstrInput="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 above example, string contains \r\n as new line and output was generated in 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 different environment.

About the author

rahimv

rahimv has over 15 years of experience in designing and developing Java applications. His areas of expertise are J2EE and eCommerce. If you like the website, follow him on Facebook, Twitter or Google Plus.