Java String lines() Method

The String.lines() method in Java is used to return a stream of lines extracted from a string, separated by line terminators. This method is part of the String class and was introduced in Java 11. It provides a convenient way to process multi-line strings.

Table of Contents

  1. Introduction
  2. lines Method Syntax
  3. Examples
    • Basic Usage
    • Handling Empty Lines
    • Processing Lines
    • Real-World Use Case
  4. Conclusion

Introduction

The String.lines() method is a member of the String class in Java. It allows you to split a string into lines and process them using streams. This method is particularly useful for reading and processing multi-line text data.

lines() Method Syntax

The syntax for the lines method is as follows:

public Stream<String> lines()

Examples

Basic Usage

The lines method can be used to split a string into lines and create a stream of lines.

Example

import java.util.stream.Stream;

public class LinesExample {
    public static void main(String[] args) {
        String str = "Hello,\nWorld!\nWelcome to Java.";
        Stream<String> lines = str.lines();

        lines.forEach(System.out::println);
    }
}

Output:

Hello,
World!
Welcome to Java.

Handling Empty Lines

The lines method correctly handles empty lines within the string.

Example

import java.util.stream.Stream;

public class LinesEmptyLinesExample {
    public static void main(String[] args) {
        String str = "Hello,\n\nWorld!\n\nWelcome to Java.";
        Stream<String> lines = str.lines();

        lines.forEach(line -> System.out.println("Line: '" + line + "'"));
    }
}

Output:

Line: 'Hello,'
Line: ''
Line: 'World!'
Line: ''
Line: 'Welcome to Java.'

Processing Lines

You can use the lines method to process each line of the string, such as trimming whitespace or filtering out empty lines.

Example

import java.util.stream.Stream;

public class ProcessLinesExample {
    public static void main(String[] args) {
        String str = "Hello,\n  World!  \n\n Welcome to Java. ";

        Stream<String> lines = str.lines()
                                  .map(String::trim)
                                  .filter(line -> !line.isEmpty());

        lines.forEach(System.out::println);
    }
}

Output:

Hello,
World!
Welcome to Java.

Real-World Use Case

Example: Reading and Processing a Multi-Line String

One common use case for lines is reading and processing multi-line string data, such as logs or user input.

import java.util.stream.Stream;

public class ProcessLogsExample {
    public static void main(String[] args) {
        String logs = "INFO: Application started\nERROR: Null pointer exception\nWARN: Deprecated API usage\nINFO: Application stopped";

        Stream<String> lines = logs.lines();

        lines.filter(line -> line.startsWith("ERROR"))
             .forEach(System.out::println);
    }
}

Output:

ERROR: Null pointer exception

In this example, the lines method is used to split the log data into lines, and then the stream is filtered to only include lines that start with "ERROR".

Conclusion

The String.lines() method in Java is used for splitting a string into lines and processing them using streams. It provides a convenient way to handle multi-line text data and perform various operations on each line. By understanding and utilizing the lines method, you can efficiently manage and process multi-line strings in your Java programs.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top