String Strip Versus Trim Transforms
Normalize user text with strip, stripLeading, stripTrailing, and isBlank, and build separators with repeat — strip handles Unicode spaces trim misses.
Code
public class DisplayNameNormalizer {
public static void main(String[] args) {
String raw = " Ada Lovelace ";
// strip 与 trim 的差别:trim 只删 <= U+0020 的字符,strip 认整个 Unicode 空白表
System.out.println("strip [" + raw.strip() + "] trim [" + raw.trim() + "]");
String enQuad = "\u2000Ada\u2000"; // EN QUAD:trim 视而不见,strip 会移除
System.out.println("trim keeps [" + enQuad.trim() + "]");
System.out.println("strip drops [" + enQuad.strip() + "]");
System.out.println("leading only [" + enQuad.stripLeading() + "]");
System.out.println("trailing only [" + enQuad.stripTrailing() + "]");
// isBlank:空串与纯空白都算 blank
System.out.println("blank check: " + " \t ".isBlank() + " / " + "x".isBlank());
// repeat 造分隔线与缩进,比手写循环清晰
System.out.println("-".repeat(24));
System.out.println("na".repeat(3));
}
}How it works
Count the class DisplayNameNormalizer and the function main as the entire surface area of String Strip Versus Trim Transforms.
Putting it to use
Each run prints the 7 labeled blocks strip [, trim keeps [, strip drops [, leading only [, trailing only [, blank check:, na in source order.
Core lines
String enQuad = "\u2000Ada\u2000"; // EN QUAD:trim 视而不见,strip 会移除
System.out.println("trim keeps [" + enQuad.trim() + "]");
String raw = " Ada Lovelace ";
Interactive Tools for This Topic
FAQ
- What does String Strip Versus Trim Transforms print when run?
- Invoke the file as-is — the `strip [`, `trim keeps [`, `strip drops [` prefixes identify which step printed each line.
Report a problem with this exampleOpens a pre-filled issue on GitHub.