Skip to content

Instantly share code, notes, and snippets.

@blankyao
Created March 2, 2011 02:31
Show Gist options
  • Save blankyao/850360 to your computer and use it in GitHub Desktop.
Save blankyao/850360 to your computer and use it in GitHub Desktop.
string.trim()
String.prototype.trim = function() {
var str = this,
whitespace = ' \n\r\t\f\x0b\xa0\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u200b\u2028\u2029\u3000';
for (var i = 0,len = str.length; i < len; i++) {
if (whitespace.indexOf(str.charAt(i)) === -1) {
str = str.substring(i);
break;
}
}
for (i = str.length - 1; i >= 0; i--) {
if (whitespace.indexOf(str.charAt(i)) === -1) {
str = str.substring(0, i + 1);
break;
}
}
return whitespace.indexOf(str.charAt(0)) === -1 ? str : '';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment