How to split a string by new line

In this post we will provide an example of how to split a string by new line in Java.

How to split a string by new line

The new line character is different depending on where the file was created. The list below shows the new line character for each operating system.

  • Windows uses \r\n
  • Unix uses \n
  • Max uses \r or \n for new versions

The example below uses the String.split() method to split the string based on a regular expression.

Running the above code produces the following:

Similar posts

  1. String to InputStream in Java
  2. Java 8 Comparator example
  3. String to an Integer in Java
  4. How to loop through a list in Java

References

  1. String split API Documentation

 

 

Leave a Reply