String.lines()
method in Java is used to split a string into a stream of lines. This guide will cover the method's usage, explain how it works, and provide examples to demonstrate its functionality.Table of Contents
- Introduction
lines
Method Syntax- Examples
- Splitting a String into Lines
- Processing Lines in a Stream
- Handling Empty Lines
- Conclusion
Introduction
The String.lines()
method is a member of the String
class in Java, introduced in Java 11. It allows you to split a string into a stream of lines, making it easier to process multiline strings efficiently.
lines
Method Syntax
The syntax for the lines
method is as follows:
public Stream<String> lines()
Examples
Splitting a String into Lines
The lines
method can be used to split a string into a stream of lines.
Example
import java.util.stream.Stream;
public class LinesExample {
public static void main(String[] args) {
String multilineString = "Hello, World!\nWelcome to Java.\nEnjoy coding!";
Stream<String> lines = multilineString.lines();
lines.forEach(System.out::println);
}
}
Output:
Hello, World!
Welcome to Java.
Enjoy coding!
Processing Lines in a Stream
The lines
method returns a stream, allowing you to process each line using stream operations.
Example
import java.util.stream.Stream;
public class LinesExample {
public static void main(String[] args) {
String multilineString = "Hello, World!\nWelcome to Java.\nEnjoy coding!";
Stream<String> lines = multilineString.lines();
long lineCount = lines.count();
System.out.println("Number of lines: " + lineCount);
}
}
Output:
Number of lines: 3
Handling Empty Lines
The lines
method will include empty lines in the resulting stream.
Example
import java.util.stream.Stream;
public class LinesExample {
public static void main(String[] args) {
String multilineString = "Hello, World!\n\nWelcome to Java.\n\nEnjoy coding!";
Stream<String> lines = multilineString.lines();
lines.forEach(line -> System.out.println("Line: '" + line + "'"));
}
}
Output:
Line: 'Hello, World!'
Line: ''
Line: 'Welcome to Java.'
Line: ''
Line: 'Enjoy coding!'
Counting Non-Empty Lines
To count only non-empty lines, you can filter out the empty lines.
Example
import java.util.stream.Stream;
public class LinesExample {
public static void main(String[] args) {
String multilineString = "Hello, World!\n\nWelcome to Java.\n\nEnjoy coding!";
long nonEmptyLineCount = multilineString.lines()
.filter(line -> !line.isBlank())
.count();
System.out.println("Number of non-empty lines: " + nonEmptyLineCount);
}
}
Output:
Number of non-empty lines: 3
Conclusion
The String.lines()
method in Java is used for splitting a string into a stream of lines. By understanding how to use this method, you can efficiently process multiline strings in your Java applications. Whether you are splitting a string into lines, processing each line in a stream, handling empty lines, or counting non-empty lines, the lines
method provides a reliable solution for these tasks.
Comments
Post a Comment
Leave Comment