Skip to main content
Java 11 (LTS) · Topic 1 of 6

New String methods

Definition​

Java 11 added useful String methods: isBlank (empty or only spaces), strip (trim that understands Unicode), lines (split into lines as a stream) and repeat.

Example code​

The four new String helpers

class Demo {
public static void main(String[] args) {
System.out.println(" ".isBlank()); // true
System.out.println("[" + " hi ".strip() + "]"); // [hi]
System.out.println("ab".repeat(3));

"first\nsecond\nthird".lines()
.map(line -> "> " + line)
.forEach(System.out::println);
}
}

// Output:
// true
// [hi]
// ababab
// > first
// > second
// > third

Finished this page? Track your progress (saved only in your browser).