Do you need to include a trim function in your javascript code?
No worries. Include the code snippet below at the beginning of your script and then you can trim any string str in your javascript code by simply calling str.trim()
String.prototype.trim=function()
{
var s=this.replace(/^ss*/, ''),b=/s/,n=s.length;
while(b.test(s.charAt(--n))); return s.slice(0,n+1);
}
The above method works by removing leading white spaces using regular expressions, and trailing white spaces using character comparison and is one of the fastest approaches.
An example implementation would be as follows:
var name=" India "; name=name.trim();







No Comments Yet - be the First!