alain.janinm alain.janinm - 2 years ago 84
Java Question

What is the best way to iterate over the lines of a Java String?

Currently I'm using something like :

String[]lines = textContent.split(System.getProperty("line.separator"));
for(String tmpLine : lines){
//do something

I'm not very glad of this method because it create an heavy array (let say
can contain a book).

Is there any better solution to iterate over the lines of a

Answer Source

You could use :

BufferedReader bufReader = new BufferedReader(new StringReader(textContent));

And use the readLine() method :

String line=null;
while( (line=bufReader.readLine()) != null )

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download