
- Java 11 Tutorial
- Java 11 - Home
- Java 11 - Overview
- Java 11 - Environment Setup
- Java 11 - Standard HttpClient
- Java 11 - Compile free Launch
- Java 11 - String APIs
- Java 11 - Collections to Array
- Java 11 - File APIs
- Java 11 - Optional Class
- Java 11 - Not Predicate
- Java 11 - var in lambda
- Java 11 - Nest Based Access
- Java 11 - Removed/Deprecated API
- Java Other Versions Tutorials
- Java Tutorial
- Java 8 Tutorial
- Java 9 Tutorial
- Java 10 Tutorial
- Java 12 Tutorial
- Java 13 Tutorial
- Java 14 Tutorial
- Java 15 Tutorial
- Java 16 Tutorial
- Java 11 Useful Resources
- Java 11 - Quick Guide
- Java 11 - Useful Resources
- Java 11 - Discussion
Java 11 - String API
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 trailing whitespaces.
String.stripLeading() − Removes the leading whitespaces.
String.stripTrailing() − Removes the trailing whitespaces.
String.lines() − Return the stream of lines of multi-line string.
Consider the following example −
ApiTester.java
import java.util.ArrayList; import java.util.List; public class APITester { public 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.stripTrailing()); // " abc" sample = "This\nis\na\nmultiline\ntext."; List<String> lines = new ArrayList<>(); sample.lines().forEach(line -> lines.add(line)); lines.forEach(line -> System.out.println(line)); } }
Output
abc abc false true true abc abc abc This is a multiline text.
Advertisements