Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

Java String strip: Removing Unicode Whitespace

Learn how Java's String.strip() removes Unicode whitespace, how it differs from trim(), and when to use each method in practice.

~3 min readJava
#java#string-manipulation#unicode#java-11
Illustration showing Java String.strip() removing Unicode whitespace characters from both ends of a string, contrasted with trim() leaving them in place.

When Java 11 introduced String.strip(), it gave developers a way to remove leading and trailing whitespace using Unicode-aware whitespace detection. strip() is often compared to the older trim() method, but the two differ in which characters they treat as whitespace. Understanding that difference matters when your input comes from external systems, user input, or text files that may contain non-ASCII whitespace.

What strip() Removes and How It Works

strip() returns a String with leading and trailing whitespace removed. It uses Character.isWhitespace() to decide what counts as whitespace. The original string is not modified; if no whitespace is present at either end, the result has the same characters as the input.

java
String input = " hello world "; String result = input.strip(); System.out.println(result); // "hello world"

The whitespace definition in Character.isWhitespace() includes Unicode space characters such as em space (U+2003), ideographic space (U+3000), and line separators (U+2028), along with the standard tab, newline, and carriage return characters. This makes strip() the correct choice when input may contain Unicode whitespace.

strip() vs trim(): The Core Difference

The trim() method, which has existed since Java 1.0, removes any character with a code point less than or equal to U+0020 (the space character). That includes ASCII control characters like null (U+0000) and bell (U+0007), but it does not include Unicode whitespace characters with higher code points.

Behaviortrim()strip()
Whitespace definitionCode point <= U+0020Character.isWhitespace()
Removes em space (U+2003)NoYes
Removes ideographic space (U+3000)NoYes
Removes null character (U+0000)YesNo
Available sinceJava 1.0Java 11
java
String input = "\u2003hello\u2003"; // em space around hello System.out.println(input.trim()); // "\u2003hello\u2003" unchanged System.out.println(input.strip()); // "hello"

The practical consequence is that trim() leaves Unicode whitespace in place, while strip() removes it. Conversely, trim() removes control characters that strip() leaves untouched because Character.isWhitespace() returns false for them.

Using stripLeading() and stripTrailing()

Java 11 also added stripLeading() and stripTrailing(), which remove whitespace from only one side of the string. These use the same whitespace definition as strip().

java
String input = " hello "; String leading = input.stripLeading(); // "hello " String trailing = input.stripTrailing(); // " hello"

These methods are useful when the format of the input requires preserving whitespace on one side. For example, when parsing a fixed-width field where trailing whitespace is significant, stripLeading() can remove padding without altering the content that follows.

Choosing Between strip() and trim()

Use strip() when the input may contain Unicode whitespace. This is common with user-generated content, text imported from other locales, or data read from files that use non-ASCII space characters. Use trim() when you specifically need to remove ASCII control characters, or when you are working in a Java version older than 11 and cannot use strip().

Performance is rarely a reason to pick one over the other; the choice should be driven by the character set of your input.

Compatibility and Runtime Behavior

strip() requires Java 11 or later. If your codebase targets Java 8 or earlier, strip() will not compile. The trim() method remains the only standard String method in those environments for this purpose.

Both methods treat strings as immutable and return a String result. If you need to detect whether whitespace was present, compare the stripped value with equals() rather than relying on ==, because the API does not guarantee that the original instance is returned when no whitespace is found.

Edge Cases and Common Misconceptions

A common misconception is that strip() removes all Unicode whitespace. It does not. Character.isWhitespace() explicitly excludes non-breaking spaces such as U+00A0 (no-break space), U+2007 (figure space), and U+202F (narrow no-break space). Neither strip() nor trim() removes these characters.

java
String input = "\u00A0hello\u00A0"; // no-break space System.out.println(input.strip()); // "\u00A0hello\u00A0" unchanged System.out.println(input.trim()); // "\u00A0hello\u00A0" unchanged

If you need to remove non-breaking spaces, you must handle them explicitly, for example by replacing them before calling strip():

java
String cleaned = input.replace('\u00A0', ' ').strip();

Another edge case is the empty string. Calling strip() on an empty string returns an empty string, and calling it on a string that contains only whitespace also returns an empty string.

Try matching browser tools

All tools

Small utilities that fit Java work — they run entirely in this browser tab, so nothing you paste leaves your device.

All Java guides

More in Java

All Java guides

The guides filed next to this one in Java, in library order.