In Java, strip() returns a string with all leading and trailing white space removed, where white space means any code point for which Character.isWhitespace returns true. That includes Unicode spaces such as U+2003 and U+3000.
It is the modern, Unicode-aware replacement for trim(). Use it for text that may come from the web, from documents, or from other languages. Like every String method, it leaves the original untouched.
strip() is an instance method of java.lang.String, added in Java 11. Call it on a string, as in text.strip(); calling it on a null reference throws NullPointerException. Strings are immutable, so it never changes the original: it returns a new String (or the same one when nothing changes). Keep the result. Requires Java 11 or newer.
Syntax
Call it with no arguments and keep the result.
java
String clean = input.strip();
String left = input.stripLeading(); // only the start
String right = input.stripTrailing(); // only the end
Call
What it does
" Java ".strip()
"Java".
"\u3000x\u3000".strip()
"x": trim() would keep these.
"\u00A0x".strip()
"\u00A0x": no-break space is kept.
Character.isWhitespace deliberately excludes the no-break spaces U+00A0, U+2007, and U+202F. Remove those explicitly with replace('\u00A0', ' ') before stripping if your input contains them.
Library declarations
java
public String strip();
Parameters
Parameter
Description
None
strip() takes no parameters.
Return Value
Return Value
Description
String
The text without leading and trailing white space, or this string if there is none.
Java Examples
Example 1 — Strip Spaces, Tabs, and Line Breaks
Clean a padded label and a value surrounded by mixed whitespace.
java
public class StripBasic {
public static void main(String[] args) {
String label = " Java 11 ";
String value = "\t\n x \r\n";
System.out.println("[" + label.strip() + "]");
System.out.println("[" + value.strip() + "]");
System.out.println("[" + label + "]");
}
}
Output
[Java 11]
[x]
[ Java 11 ]
How It Works
Edges only. The two spaces on each side of "Java 11" go; the inner space stays.
Any mix of whitespace. Tabs, spaces, carriage returns, and newlines around the x are all removed.
Original untouched. Printing label again shows the padding, because strip() returned a new String.
Example 2 — Unicode Spaces: strip() Beats trim()
The ideographic space U+3000, common in East Asian text, is whitespace but above U+0020.
java
public class StripVsTrim {
public static void main(String[] args) {
String s = "\u3000Hello\u3000";
System.out.println("trim(): length " + s.trim().length());
System.out.println("strip(): length " + s.strip().length());
System.out.println("strip() result: " + s.strip());
}
}
No-break spaces survive. U+00A0 is excluded from Java white space, so strip() leaves the length at 6.
trim() removes low control chars. NUL (U+0000) is below U+0020, so trim() cuts it from both ends, leaving 4.
strip() keeps NUL. NUL is not white space, so strip() does not touch it; pick the method that matches your data.
Frequently Asked Questions
It removes leading and trailing white space, as defined by Character.isWhitespace, and returns the result as a new String.
trim() removes chars up to U+0020. strip() removes Unicode white space such as U+2003 and U+3000, but keeps non-whitespace control characters.
Java 11, together with stripLeading(), stripTrailing(), isBlank(), lines(), and repeat().
No. U+00A0, U+2007, and U+202F are not white space for Character.isWhitespace. Replace them first if needed.
For human text, strip() is usually the better choice because it understands Unicode spaces. Use trim() when you must run on Java 8 or also want to drop control characters.