rtrim, ltrim, and trim methods for String





9
Date Submitted Sat. Mar. 10th, 2007 6:44 PM
Revision 1 of 1
Helper albud
Tags JavaScript | strings
Comments 0 comments
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())

 

Allain Lalonde

Comments

There are currently no comments for this snippet.

Voting