00:00

New String Methods in Java 11

Last updated: · Standard (Java 11)

Java 11 added six small but very useful methods to String: isBlank(), strip(), stripLeading(), stripTrailing(), lines() and repeat(int). They replace common helper code that projects used to write themselves or import from libraries.

This tutorial explains each method, the important difference between strip() and trim(), and a practical clean-up example.

1. Why Were These Methods Added?

Checking for whitespace-only input, splitting text into lines and padding text are everyday tasks. Before Java 11 they needed regular expressions, loops or libraries such as Apache Commons. trim() also had a hidden limitation: it only understands ASCII whitespace.

2. Method Summary

MethodReturnsDescription
isBlank()booleantrue if empty or only whitespace
strip()Stringremoves leading and trailing whitespace (Unicode-aware)
stripLeading() / stripTrailing()Stringremoves whitespace at one end only
lines()Stream<String>splits on \n, \r or \r\n
repeat(int count)Stringthe string repeated count times

3. Practical Examples

Example 1: Each method in action

The second string starts and ends with the Unicode em space (U+2003). trim() keeps it, while strip() removes it.

NewStringMethods.java
public class NewStringMethods {
    public static void main(String[] args) {
        String spaces = "   ";
        String padded = "  Java 11  ";          // contains Unicode em spaces

        // isBlank(): true for empty or whitespace-only strings
        System.out.println("isEmpty  : " + spaces.isEmpty() + ", isBlank: " + spaces.isBlank());

        // strip() is Unicode-aware; trim() only removes characters <= U+0020
        System.out.println("trim()   : [" + padded.trim() + "]");
        System.out.println("strip()  : [" + padded.strip() + "]");
        System.out.println("leading  : [" + "  left".stripLeading() + "], trailing: [" + "right  ".stripTrailing() + "]");

        // repeat(n)
        System.out.println("repeat   : " + "=-".repeat(6));

        // lines(): a Stream of lines, handling \n, \r and \r\n
        String report = "id,item\r\n1,pen\n2,ink";
        report.lines().map(line -> "  > " + line).forEach(System.out::println);
        System.out.println("line count: " + report.lines().count());
    }
}
Output
isEmpty  : false, isBlank: true
trim()   : [  Java 11  ]
strip()  : [Java 11]
leading  : [left], trailing: [right]
repeat   : =-=-=-=-=-=-
  > id,item
  > 1,pen
  > 2,ink
line count: 3

Example 2: Cleaning pasted input

lines(), strip() and isBlank() work well together in a stream, and repeat() builds simple text layouts.

CleanInput.java
import java.util.List;
import java.util.stream.Collectors;

public class CleanInput {
    public static void main(String[] args) {
        // A pasted block of tags with blank lines and stray spaces
        String pasted = "  java \n\n   spring\t\n \n kafka  \n";

        List<String> tags = pasted.lines()
                .map(String::strip)
                .filter(line -> !line.isBlank())
                .collect(Collectors.toList());
        System.out.println("Tags: " + tags);

        // Build a simple text table with repeat()
        String border = "+" + "-".repeat(12) + "+";
        System.out.println(border);
        for (String tag : tags) {
            System.out.println("| " + tag + " ".repeat(11 - tag.length()) + "|");
        }
        System.out.println(border);
    }
}
Output
Tags: [java, spring, kafka]
+------------+
| java       |
| spring     |
| kafka      |
+------------+

4. strip() vs trim()

trim compared with strip trim removes characters with code point up to U+0020 only, so Unicode spaces such as U+2003 stay. strip uses Character.isWhitespace and removes them. TRIM() "\u2003 Java 11 \u2003" input removes <= U+0020 ASCII rule " Java 11 " em spaces remain STRIP() "\u2003 Java 11 \u2003" input Character.isWhitespace Unicode rule "Java 11" fully cleaned
Figure 1: Prefer strip() for user input, which may contain Unicode whitespace.

5. Important Points and Best Practices

  • Prefer strip() over trim() for text that may contain Unicode whitespace.
  • isBlank() is true for "" as well; isEmpty() is true only for "".
  • repeat(0) returns an empty string; a negative count throws IllegalArgumentException.
  • lines() is lazy and handles Windows (\r\n) and Unix (\n) line endings.
  • Calling these methods on null still throws NullPointerException.

6. Summary

  • Java 11 added isBlank, strip, stripLeading, stripTrailing, lines and repeat.
  • strip() is Unicode-aware; trim() only removes ASCII control characters and spaces.
  • lines() returns a lazy stream of lines.
  • repeat() replaces manual loops for building repeated text.

Related Tutorials

7. References