-3

I want specific rows from a String to print out.

String:

"
test

 test

 test

 test"

I only want the second and the third row.

How should I do this?

Mark Rotteveel
  • 90,369
  • 161
  • 124
  • 175

1 Answers1

0

If you're reading from a file, use a simple FileReader and use readLine() method. You can read line by line and take the lines/rows you are interested in.

If you have a simple string containing multiple lines, split by new line character using one of the following options:

String[] values = str.spilt("\\r?\\n|\\r");

# OR

String[] values = str.spilt("\\R");

And then take the required values from the array by index.

System.out.println(values[1]+" and "+ values[2]);
fiveelements
  • 3,343
  • 1
  • 16
  • 16