Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

extracting substring by lines

Tags:

java

I am fairly new to programming so please bear with me. Say I have a large string such as this.

String story = "This is the first line.\n"
+ "This is the second line.\n"
+ "This is the third line\n"
+ "This is the fourth line.\n"
+ "This is the fifth line.";

How would I go about extracting the first, fourth, and so on lines?

like image 626
user415663 Avatar asked Aug 10 '10 17:08

user415663


1 Answers

If you want to avoid creating arrays, you can use Scanner

Scanner scanner = new Scanner(story);
while(scanner.hasNextLine()) {
  System.out.println(scanner.nextLine());
}
like image 110
Marimuthu Madasamy Avatar answered Sep 30 '22 17:09

Marimuthu Madasamy