Java String lines() Method Tutorial

In this section, we will learn what the String lines() method is and how to use it in Java.

What is Java String lines() Method?

The Java String lines() method is used to create a stream of lines from a string value.

Basically, this method looks for newline characters in a string value and splits the entire string into multiple lines using these new line characters in order to create a stream.

Java lines() Method Syntax:

public Stream lines()

lines() Method Parameters:

This method does not take an argument.

lines() Method Return Value:

The return value of this method is a stream object that contains the lines of the target string value.

Example: using String lines() method

import java.util.stream.Stream;

public class Main {

   public static void main(String[] args) throws Exception{

      Stream stream = "This is the line one \n This is the line two \n This is the line three \n This is the line four".lines();
      stream.forEach(System.out::println);
   }
}

Output:

This is the line one

This is the line two

This is the line three

This is the line four

Leave a Reply