Browse - Programming Tips - How can I split a String into lines?

Date: 2012apr10
Language: Java

Q.  How can I split a String into lines?

A.  Use the split() method like this:

String fileContents = readFile() for (String line : fileContents.split("\\r\\n|\\n")) { // Do whatever you want with the line System.out.println(line); }
This works with Linux, Windows and Mac (Unix) line endings.