Fundamentals 8 min read

Mastering Java String Whitespace: trim, strip, replace and More

This article explains the various Java string methods for removing leading and trailing whitespace—including trim, strip, stripLeading, stripTrailing, replace, replaceAll, and replaceFirst—showing their differences, Unicode handling, and practical code examples.

FunTester
FunTester
FunTester
Mastering Java String Whitespace: trim, strip, replace and More

String manipulation, especially removing whitespace, is a common task in Java. This article reviews the built‑in methods for deleting leading and trailing spaces, from the classic trim() to the newer Unicode‑aware strip(), stripLeading() and stripTrailing(), and also covers replacement methods such as replace(), replaceAll() and replaceFirst().

Basic methods

trim()

: removes characters whose ASCII value ≤ 32 from both ends of the string. strip(): introduced in Java 11, removes all Unicode whitespace characters using Character.isWhitespace(int). stripLeading() and stripTrailing(): also added in Java 11, remove leading or trailing whitespace respectively, following the same Unicode rules.

All these methods return a new String because Java strings are immutable.

Code examples

public class FunTester {
    public static void main(String[] args) {
        String s = "    one    two    three    ";
        System.out.println("Original: \"" + s + "\"");
        System.out.println("trim(): \"" + s.trim() + "\"");
    }
}
public class StringStripTest {
    public static void main(String[] args) {
        String s = "    one    two    three    ";
        System.out.println("Original: \"" + s + "\"");
        System.out.println("strip(): \"" + s.strip() + "\"");
    }
}

Running the two programs shows that trim() leaves Unicode spaces (e.g., U+2001) untouched, while strip() removes them.

Key differences

trim() : available since Java 1, removes characters with ASCII value ≤ U+0020.

strip() : added in Java 11, removes all Unicode whitespace using Character.isWhitespace(int).

Other replacement methods

replace(CharSequence target, CharSequence replacement)

: replaces every occurrence of a literal target. replaceAll(String regex, String replacement): uses regular expressions; useful for removing all spaces ( "\\s+") or only leading ( "^\\s+") or trailing ( "\\s+$") spaces. replaceFirst(String regex, String replacement): replaces only the first match, which can be used to delete the first occurrence of a pattern.

public class StringReplaceAllTest {
    public static void main(String[] args) {
        String s = "    one    two    three    ";
        System.out.println("replaceAll(\"\\\\s+\", \"\"): \"" + s.replaceAll("\\s+", "") + "\"");
    }
}

These examples demonstrate how to choose the appropriate method depending on whether you need simple ASCII trimming, full Unicode support, or pattern‑based replacement.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaStringUnicode.trimstripWhitespacereplace
FunTester
Written by

FunTester

10k followers, 1k articles | completely useless

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.