rtrim, ltrim, and trim methods for String
9
Rather than have standalone functions rtrim, ltrim, and trim (as in http://www.bytemycode.com/snippets/snippet/397/) why not have them as methods of all string objects?
//Function to trim the space in the left side of the string
String.prototype.ltrim = function (){
return this.replace(/^\s*/, "" );
}
//Function to trim the space in the right side of the string
String.prototype.rtrim = function(){
return this.replace( /\s*$/, "" );
}
//Function to trim the space in the string
String.prototype.trim = function() {
return this.rtrim().ltrim();
}
alert(" Hello ".trim())






There are currently no comments for this snippet.