Skip to content

Java split String by new line Example

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.

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,

The above pattern will cover all three OS newline characters.

Example

Output

How to include empty trailing lines?

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

Output

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.

Output

How to ignore empty lines in between?

Sometimes string or file contains empty lines between the content as given below.

Output

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,

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 ([]).

Output

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.

Output

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.

Output

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.

About the author

Leave a Reply

Your email address will not be published.