Handy String Methods





8
Date Submitted Tue. Mar. 6th, 2007 7:26 PM
Revision 1 of 1
Helper albud
Tags JavaScript | String
Comments 1 comments
I like to have these two methods in my toolbelt, even though it's syntactic sugar, I think it aids clarity.


String.prototype.left = function (n) {
        if (n > this.length)
                return this;
        return this.substr(0, n);
}

String.prototype.right = function(n) {
        if (n > this.length)
                return this;
               
        return this.substr(this.length-n, n);
}

 

alert("Hello".right(2)) // Yields lo
alert("Hello".left(4))  // Yields Hell

 

Allain Lalonde

Comments

Comments nice
Wed. Apr. 4th, 2007 5:31 AM    Syntax Master sundaramkumar

Voting