Dave's Brain

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.

Add a comment

Sign in to add a comment
Copyright © 2008-2018, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.