Strings beginner

String.strip() vs trim()

Use Unicode-aware stripping with strip(), stripLeading(), stripTrailing().

✕ Java 8
// trim() only removes ASCII whitespace
// (chars <= U+0020)
String clean = str.trim();
✓ Java 11+
// strip() removes all Unicode whitespace
String clean = str.strip();
String left  = str.stripLeading();
String right = str.stripTrailing();
🌐

Unicode-correct

Handles all whitespace characters from every script.

🎯

Directional

stripLeading() and stripTrailing() for one-sided trimming.

🛡️

Fewer bugs

No surprise whitespace left behind in international text.

Old Approach
trim()
Modern Approach
strip()
Since JDK
11
Difficulty
beginner
String.strip() vs trim()
Available

Widely available since JDK 11 (Sept 2018)

How it works

trim() only removes characters ≤ U+0020 (ASCII control chars and space). strip() uses Character.isWhitespace() which handles Unicode spaces like non-breaking space, ideographic space, etc.

Share 𝕏 🦋 in