-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathstring-strip.yaml
More file actions
46 lines (46 loc) · 1.48 KB
/
string-strip.yaml
File metadata and controls
46 lines (46 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
---
id: 30
slug: "string-strip"
title: "String.strip() vs trim()"
category: "strings"
difficulty: "beginner"
jdkVersion: "11"
oldLabel: "Java 8"
modernLabel: "Java 11+"
oldApproach: "trim()"
modernApproach: "strip()"
oldCode: |-
// trim() only removes ASCII whitespace
// (chars <= U+0020)
String clean = str.trim();
modernCode: |-
// strip() removes all Unicode whitespace
String clean = str.strip();
String left = str.stripLeading();
String right = str.stripTrailing();
summary: "Use Unicode-aware stripping with strip(), stripLeading(), stripTrailing()."
explanation: "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."
whyModernWins:
- icon: "🌐"
title: "Unicode-correct"
desc: "Handles all whitespace characters from every script."
- icon: "🎯"
title: "Directional"
desc: "stripLeading() and stripTrailing() for one-sided trimming."
- icon: "🛡️"
title: "Fewer bugs"
desc: "No surprise whitespace left behind in international text."
support:
state: "available"
description: "Widely available since JDK 11 (Sept 2018)"
prev: "strings/string-isblank"
next: "strings/string-repeat"
related:
- "strings/string-formatted"
- "strings/string-indent-transform"
- "strings/string-chars-stream"
docs:
- title: "String.strip()"
href: "https://docs.oracle.com/en/java/javase/25/docs/api/java.base/java/lang/String.html#strip()"