How to split String by newline in java

Split String by new line in java

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.

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.

Output:

[This is a simple String, , , Let’s try to split this, One more line]

If you don’t want empty space in between the line, you can use [\\r?\\n|\\r] as regex.
Here is an example:

[This is a simple String, Let’s try to split this, One more line]

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.

Output:

[First Line, Second Line]

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.

Output:

[First Line, Second Line]

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.

Output:

[First Line, Second Line, Third Line]

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.

Output:

[First Line, Second Line, Third Line]

That’s all about how to split String by newline in java.

Was this post helpful?

Related Posts

  • 28 October

    Convert UUID to String in Java

    Table of ContentsIntroductionUUID class in JavaConvert UUID to String in Java Introduction In this article, we will have a look on How to Convert UUID to String in Java. We will also shed some light on the concept of UUID, its use, and its corresponding representation in Java class. Let us have a quick look […]

  • 27 September

    Repeat String N times in Java

    Table of ContentsIntroductionWhat is a String in Java?Repeat String N times in JavaSimple For Loop to Repeat String N Times in JavaUsing Recursion to Repeat String N Times in JavaString.format() method to Repeat String N Times in JavaUsing String.repeat() method in Java 11 to Repeat String N TimesRegular Expression – Regex to Repeat String N […]

  • 15 September

    How to Replace Space with Underscore in Java

    Table of ContentsReplace space with underscore in java1. Using replace() method2. Using replaceAll() method Learn about how to replace space with underscore in java. Replace space with underscore in java 1. Using replace() method Use String’s replace() method to replace space with underscore in java. String’s replace() method returns a string replacing all the CharSequence […]

  • 15 September

    How to Replace Comma with Space in Java

    Table of ContentsReplace comma with space in java1. Using replace() method2. Using replaceAll() method Learn about how to replace comma with space in java. Replace comma with space in java 1. Using replace() method Use String’s replace() method to replace comma with space in java. Here is syntax of replace() method: [crayon-63dc565aac22d932392391/] [crayon-63dc565aac233785039774/] Output: 1 […]

  • 11 June

    Remove Parentheses From String in Java

    Table of ContentsJava StringsRemove Parentheses From a String Using the replaceAll() MethodRemove Parentheses From a String by TraversingConclusion Java uses the Strings data structure to store the text data. This article discusses methods to remove parentheses from a String in Java. Java Strings Java Strings is a class that stores the text data at contiguous […]

  • Escape percent sign in java
    08 February

    Escape percent sign in String’s format method in java

    Table of ContentsEscape Percent Sign in String’s Format Method in JavaEscape Percent Sign in printf() Method in Java In this post, we will see how to escape Percent sign in String’s format() method in java. Escape Percent Sign in String’s Format Method in Java String’s format() method uses percent sign(%) as prefix of format specifier. […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.