Prev Next

Java / Java 11

What is String.lines() API in Java 11?

The lines() method is a static method that returns a stream of lines extracted from a given multi-line string, separated by line terminators.

public Stream<String> lines()

The stream returned by lines() method contains the lines from this string in the same order in which they occur in the multi-line.

import java.util.ArrayList;
import java.util.List;
 
public class JavaStringLinesAPIExample {
 
    public static void main(String[] args) {
        String inputStr = "\nOne\nTwo\nThree\r\nFour\n";
 
        List<String> lines = new ArrayList<>();
        inputStr.lines().forEach(s -> lines.add(s));
 
        System.out.println(lines);
    }
 
}

More Related questions...

Show more question and Answers...


Comments & Discussions