While working with string in javascript sometimes we need to remove whitespace from the beginning and end of a string. Just do something like this:
String.prototype.trim=function(){ return this.replace(/^\s*|\s*$/g,”); } String.prototype.ltrim=function(){ return this.replace(/^\s*/g,”); } String.prototype.rtrim=function(){ return this.replace(/\s*$/g,”); }
How to use:
var myString = ‘   mystring     ‘;
sTrim=myString.trim();Â Â Â Â //return ‘mystring’
sLTrim=myString.ltrim();   //return ‘mystring     ‘
sRTrim=myString.rtrim();  //return ‘   mystring’
Be First To Comment
Related Post
Leave Your Comments Below