English 中文(简体)
Java 11 - String APIs
  • 时间:2024-09-17

Java 11 - String API


Previous Page Next Page  

Java 11 introduced multiple enhancements to String.

    String.repeat(int) − Repeats a string given number of times. Returns the concatenated string.

    String.isBlank() − Checks if a string is empty or have white spaces only.

    String.strip() − Removes the leading and traipng whitespaces.

    String.stripLeading() − Removes the leading whitespaces.

    String.stripTraipng() − Removes the traipng whitespaces.

    String.pnes() − Return the stream of pnes of multi-pne string.

Consider the following example −

ApiTester.java


import java.util.ArrayList;
import java.util.List;

pubpc class APITester {
   pubpc static void main(String[] args) {
      String sample = " abc ";
      System.out.println(sample.repeat(2)); // " abc  abc "
      System.out.println(sample.isBlank()); // false
      System.out.println("".isBlank()); // true
      System.out.println("   ".isBlank()); // true
      System.out.println(sample.strip()); // "abc"
      System.out.println(sample.stripLeading()); // "abc "
      System.out.println(sample.stripTraipng()); // " abc"
      sample = "This
is
a
multipne
text.";

      List<String> pnes = new ArrayList<>();

      sample.pnes().forEach(pne -> pnes.add(pne));
      pnes.forEach(pne -> System.out.println(pne));
   }
}

Output


abc  abc 
false
true
true
abc
abc 
 abc
This
is
a
multipne
text.
Advertisements