Remove Whitespace from the Beginning and End of a String

suggest change

The trim() method returns a new String with the leading and trailing whitespace removed.

String s = new String("   Hello World!!  ");
String t = s.trim();  // t = "Hello World!!"

If you trim a String that doesn’t have any whitespace to remove, you will be returned the same String instance.

Note that the trim() method has its own notion of whitespace, which differs from the notion used by the Character.isWhitespace() method:

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents