HOWTO · Java

Trim() vs Strip() in Java

This tutorial demonstrates the differences between trim() and strip() methods in Java.

On this page

In Java, String.trim() and String.strip() both remove qualifying characters from the beginning and end of a string; neither changes characters in the middle. Their difference is the rule used to decide which characters qualify. Use trim() for its legacy rule, or strip() when Java 11 or later and the Character.isWhitespace(int) rule fit your input.

Java trim() and strip() at a glance

Detail trim() strip()
Available since Java 1.0 Java 11
Character rule Removes leading and trailing characters with code points at or below U+0020 Removes leading and trailing characters for which Character.isWhitespace(int) returns true
Declared on String String
Interior characters Unchanged Unchanged

Both methods return a string with the qualifying ends removed. For example, ordinary spaces at the start and end are removed by both methods, while spaces between words stay in place. The key difference is not that one handles every Unicode space: each method has a specific character rule.

How trim() and strip() handle whitespace

trim() removes each leading or trailing character whose numeric code point is no greater than U+0020, including the ordinary space U+0020, tabs, and carriage returns. It does not use Unicode’s general whitespace classification. A character above that range, such as U+2005, remains at the edge after trim().

strip() checks characters with Character.isWhitespace(int). This includes many whitespace characters beyond U+0020, such as U+2005 and U+2001, but it does not include every Unicode space separator. In particular, the non-breaking space U+00A0 is not removed by strip() under this rule. Therefore, do not treat strip() as a universal Unicode whitespace cleanup operation.

In either case, only qualifying characters at the two ends are removed. A tab or space between non-whitespace characters remains in the string.

Java version and API details

trim() has been available on String since Java 1.0. strip() was added to String in Java 11, so code using it needs a Java 11 or later API. strip() is a String method; it is not declared by the CharSequence interface. The Java String API documents both methods, and the Java Character.isWhitespace(int) API defines the character test used by strip(). If an application must compile against an older Java API, use trim() or implement the exact character policy the application requires.

Compare the methods with a Java example

Run this example with Java 11 or later. It compares the results for ordinary spaces, a tab and carriage return, U+2005, U+2001, and the non-breaking space U+00A0. The source uses Unicode escapes so the tested code points are unambiguous.

public class TrimVsStrip {
    public static void main(String[] args) {
        String[] samples = {
            " www.delftstack.com ",
            "\t www.delftstack.com \r",
            "www.delftstack.com \u2005",
            "\u2001",
            "\u00A0"
        };

        for (String sample : samples) {
            System.out.println(sample.trim().equals(sample.strip()));
        }
    }
}
true
true
false
false
true

The output compares each string after trim() and strip(), in the same order as the samples above. The first two results are true because both methods remove ordinary spaces, tabs, and carriage returns at the ends. The next two are false: strip() removes U+2005 and U+2001 under Character.isWhitespace(int), while trim() retains them. The final result is true because neither method removes the non-breaking space U+00A0. The U+2001 sample contains only that character, so trim() leaves a nonempty string while strip() returns an empty string.

Choose the right method

Choose strip() when Java 11 or later is available and its Character.isWhitespace(int) behavior matches the characters your application should remove. Choose trim() when you need its narrower at-or-below-U+0020 rule or need compatibility with older Java versions. If your input policy must include characters such as non-breaking space, define and apply that policy explicitly; neither method should be assumed to cover every Unicode whitespace character.