Skip to content
Sandny Blog
  • Home
  • Java
  • JavaScript
  • AWS
  • Embedded
  • About
String.lines() Java

How to convert a string into a stream of…

  • July 29, 2021August 1, 2021
  • by Coder Kai

Java 11 adds a few new methods to the String class – isBlank, lines, strip, stripLeading, stripTrailing, and repeat. Let’s see how we can make use of the String.lines().

Available from Java 11.

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

/**
returns a stream of lines extracted from a given multi-line string
line implies that an empty string has zero lines and that there is no empty line following a line terminator at the end of a string
**/

public Stream<String> lines()
Example

import java.io.IOException;
import java.util.stream.Stream;

public class Main {
  public static void main(String[] args) {
    try {
      String str = "Line 1\n Line 2\n Line 3\n Line 4";

      Stream lines = str.lines();

      lines.forEach(System.out::println);
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}

Output

Line 1
Line 2
Line 3
Line 4

How to use IntersectionObserver to detect the position in React
How to optimize React.js app load time using react lazy
Coder Kai
A humble developer

Related articles

How to avoid NullPointerException in Java
How to avoid NullPointerException in…
The new features in Java 14
JDK 14 – The new…
Java 13 features
Java 13 (JDK 13) -The…
Java 13 Switch Expressions
How to enable preview feature in Java 13 with maven.
Enable the Preview Feature in…
create a Vertx Eventbus js client
How to create a Vertx…
neo4j boltdriver
How to create a neo4j…
Servlet File Upload
Servlet File Upload
Java RMI Slider
JAVA RMI Observer Slider

Categories

  • android 3
  • Apollo Client 1
  • AWS 8
    • AppSync 5
    • EC2 1
    • EKS 1
    • Route53 1
    • S3 1
  • AWS Amplify 1
  • Chakra UI 1
  • Docker 1
  • Embedded 1
  • EmberJS 1
  • FCM 1
  • Godaddy 1
  • GraphQL 3
  • ios 1
  • Jasper 1
  • Java 10
    • Java 11 1
    • Java 14 1
  • JavaEE 2
  • JavaScript 39
    • Express.js 4
    • Javascript Guide 7
    • Node.js 3
    • react-native 4
    • React.js 17
    • Typescript 1
  • Kubernetes 1
  • machine learning 1
  • Maven 2
  • OCaml 3
  • PostgreSQL 1
  • Python 2
  • react-native 4
  • ReactJS 3
  • sass 1
  • Server 6
  • spark 1
  • Terraform 2
  • Ubuntu 4
  • Uncategorized 1
  • webpack 2

Recent Comments

  • binance signup on How to use Typescript with arrays
  • LewisLousA on Add SASS in webpack React Redux project
  • melnhnmzy on Higher order component wrappers using React.js and reusable components.

Meta

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org

Archives

  • October 2022 3
  • September 2022 7
  • May 2022 1
  • December 2021 1
  • August 2021 1
  • July 2021 6
  • June 2021 3
  • February 2021 1
  • July 2020 1
  • December 2019 5
  • November 2019 6
  • October 2019 3
  • August 2019 1
  • March 2019 1
  • February 2019 1
  • January 2019 2
  • December 2018 1
  • September 2018 2
  • August 2018 1
  • June 2018 1
  • February 2018 1
  • November 2017 2
  • October 2017 5
  • September 2017 1
  • June 2017 1
  • May 2017 10
Sandny Blog space
Theme by Colorlib Powered by WordPress