DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Javacript Trim A String

04.02.2009
| 3618 views |
  • submit to reddit
        removes all leading and trailing spaces from string

str.replace(/^\s*/, "").replace(/\s*$/, "");

alternative, extend the string class
String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, ''); }
    

Comments

Snippets Manager replied on Mon, 2009/04/13 - 2:10pm

Great! I've been looking for a script that trims a string. Thanks, Sean Nintendo DS Accessories