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.

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.
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.
| Behavior | trim() | strip() |
|---|---|---|
| Whitespace definition | Code point <= U+0020 | Character.isWhitespace() |
| Removes em space (U+2003) | No | Yes |
| Removes ideographic space (U+3000) | No | Yes |
| Removes null character (U+0000) | Yes | No |
| Available since | Java 1.0 | Java 11 |
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().
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.
String input = "\u00A0hello\u00A0"; // no-break space
System.out.println(input.strip()); // "\u00A0hello\u00A0" unchanged
System.out.println(input.trim()); // "\u00A0hello\u00A0" unchangedIf you need to remove non-breaking spaces, you must handle them explicitly, for example by replacing them before calling strip():
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 toolsSmall utilities that fit Java work — they run entirely in this browser tab, so nothing you paste leaves your device.
Related Java guides
All Java guidesJava Final Keyword: Usage and Behavior
How Java's final keyword works for variables, methods, and classes, with practical examples and decision guidance for using it.
Java Final Variable: Syntax, Behavior, and Use Cases
Understand what a final variable in Java means, how initialization works, and when to use it for constants, immutability, and clearer code.
Java Duration vs Period: When to Use Each
java duration vs period: Compare Java Duration and Period to handle time-based and date-based calculations correctly. Learn syntax, conversion, and pitfalls.
Instance vs Static Variables in Java: Key Differences
Understand the difference between instance and static variables in Java, including memory, lifecycle, and when to use each.
More in Java
All Java guidesThe guides filed next to this one in Java, in library order.