Java 25 (LTS) reference · String Methods
Java String strip() Method
Remove leading and trailing whitespace, including Unicode spaces. Prefer it to the older trim().
Java 25 (LTS) Since Java 11
Syntax
string.strip()
Return value
A new String.
Examples
public class Main {
public static void main(String[] args) {
String raw = " [email protected] ";
System.out.println("[" + raw.strip() + "]");
System.out.println("[" + raw.stripLeading() + "]");
System.out.println("[" + raw.stripTrailing() + "]");
}
}
Output
[[email protected]] [[email protected] ] [ [email protected]]
Run this on your computer — see “Get Started” for setup.
public class Main {
public static void main(String[] args) {
String s = "\u2003padded\u2003"; // an em space
System.out.println("trim: [" + s.trim() + "]");
System.out.println("strip: [" + s.strip() + "]");
}
}
Output
trim: [ padded ] strip: [padded]
Run this on your computer — see “Get Started” for setup.
Version history
| Version | Change |
|---|---|
| Java Java 11 | Added |
Related
string.isBlank()· Return true if the string is empty or contains only whitespace. isEmpty() is true only for "".- Java Strings · tutorial