- Java.lang - Home
- Java.lang - Boolean
- Java.lang - Byte
- Java.lang - Character
- Java.lang - Character.Subset
- Java.lang - Character.UnicodeBlock
- Java.lang - Class
- Java.lang - ClassLoader
- Java.lang - Compiler
- Java.lang - Double
- Java.lang - Enum
- Java.lang - Float
- Java.lang - InheritableThreadLocal
- Java.lang - Integer
- Java.lang - Long
- Java.lang - Math
- Java.lang - Number
- Java.lang - Object
- Java.lang - Package
- Java.lang - Process
- Java.lang - ProcessBuilder
- Java.lang - Runtime
- Java.lang - RuntimePermission
- Java.lang - SecurityManager
- Java.lang - Short
- Java.lang - StackTraceElement
- Java.lang - StrictMath
- Java.lang - String
- Java.lang - StringBuffer
- Java.lang - StringBuilder
- Java.lang - System
- Java.lang - Thread
- Java.lang - ThreadGroup
- Java.lang - ThreadLocal
- Java.lang - Throwable
- Java.lang - Void
- Java.lang Package Useful Resources
- Java.lang - Useful Resources
- Java.lang - Discussion
Java String lines() Method
The Java String lines() method is used to retrieve a stream of lines/strings extracted from the current string. In other words, this method breaks down the string into lines if the current string has line terminators.
The different line terminators are '\n' which is used to insert a newline to the given string, '\r' is used to insert a carriage return in the string, '\r\n' is used to insert a carriage return followed by a newline.
A line is either a sequence of 0-n characters followed by a line terminator or, a sequence of 1n characters followed by the end of the string. The line terminator is not considered to be a part of a line.
Syntax
Following is the syntax for Java String lines() method −
public Stream<String> lines()
Parameters
This method does not accept any parameter.
Return Value
This method returns a stream of lines extracted from the given string.
Example
The following example shows the usage of Java String lines() method by extracting each line using '\n' terminator from the given string and putting it into stream using the forEach() method −
import java.util.stream.Stream;
public class StringDemo {
public static void main(String[] args) {
String s = " Tutorialspoint \n is \n a \n reputed \n edTech \n company";
Stream st = s.lines();
st.forEach(line -> System.out.println(line));
}
}
Output
Let us compile and run the above program, this will produce the following result −
Tutorialspoint is a reputed edTech company
Example
In the code below we use the lines() method to extract the stream of lines using '\r' terminator. Thereafter, we use the forEach() method for adding all these lines to a list −
import java.util.ArrayList;
import java.util.List;
public class StringDemo {
public static void main(String[] args) {
String s = "\rWelcome\rto\rtutorials\rpoint\r";
List<String> lines = new ArrayList();
s.lines().forEach(x -> lines.add(x));
System.out.println("The list of the lines are: " + lines);
}
}
Output
Let us compile and run the program above, the output will be displayed as follows:
The list of the lines are: [, Welcome, to, tutorials, point]
Example
In the example given below we are counting the number of lines in the given string using the lines() method followed by the Stream API count() method. Here, stream of lines are extracted using the '\r\n' terminator:
import java.util.stream.Stream;
public class StringDemo {
public static void main(String[] args) {
String s = "\r\nWelcome\r\nto\r\ntutorials\r\npoint";
Stream st = s.lines();
st.forEach(line -> System.out.println(line));
// counting the number of lines in the string
long count = s.lines().count();
System.out.println("The count of the lines is: " + count);
}
}
Output
On executing the program above, the output is obtained as follows −
Welcome to tutorials point The count of the lines is: 5
Example
In the following example we are using the Stream toArray() method to convert the given string lines into an array. It is to be noted that the resultant output will be an Object array and not the String array −
import java.util.Arrays;
public class StringDemo {
public static void main(String[] args) {
String s = "\rWelcome\rto\rtutorials\rpoint\r";
Object[] lines = s.lines().toArray();
System.out.println("The array of the lines are: " + Arrays.toString(lines));
}
}
Output
The output for the program above is obtained as follows −
The array of the lines are: [, Welcome, to, tutorials, point]