Java String strip() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Java 11+

Definition

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
CallWhat 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

ParameterDescription
Nonestrip() takes no parameters.

Return Value

Return ValueDescription
StringThe 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 + "]");
    }
}

How It Works

  1. Edges only. The two spaces on each side of "Java 11" go; the inner space stays.
  2. Any mix of whitespace. Tabs, spaces, carriage returns, and newlines around the x are all removed.
  3. 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());
    }
}

How It Works

  1. A full-width space on each side. The string has length 7: one U+3000, five letters, one U+3000.
  2. trim() sees nothing to remove. U+3000 is above U+0020, so the length stays 7.
  3. strip() asks Character.isWhitespace. U+3000 counts as whitespace there, so both are removed and "Hello" remains.

Example 3 — No-Break Spaces and Control Characters

Two cases where strip() keeps characters, one of which trim() would remove.

java
public class StripEdge {
    public static void main(String[] args) {
        String nbsp = "\u00A0data\u00A0";
        String nul = "\u0000data\u0000";

        System.out.println("no-break strip(): " + nbsp.strip().length());
        System.out.println("NUL trim():       " + nul.trim().length());
        System.out.println("NUL strip():      " + nul.strip().length());
    }
}

How It Works

  1. No-break spaces survive. U+00A0 is excluded from Java white space, so strip() leaves the length at 6.
  2. trim() removes low control chars. NUL (U+0000) is below U+0020, so trim() cuts it from both ends, leaving 4.
  3. 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.

Next: stripIndent()

Remove the common leading indentation from every line (Java 15+).

stripIndent() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful