At 2000-12-18 08:35 , you wrote:
is there a jscript equiv of the vbscript trim() method?
To trim leading and trailing spaces off of a string..
 
Doug Melvin
 

Yes, this is what I have:

// Returns a new trimmed string
// 'side' is which side to trim. Can be "left", "right" or "both" (default)
String.prototype.trim = function(side) {
    switch(side) {
        case "left":
            return this.replace(/^\s+/, "");
        case "right":
            return this.replace(/\s+$/, "");
        default:
            return this.replace(/^\s+|\s+$/g, "");
    }
}

// These class functions don't require 'str' to be strings as the toString() method is called
String.Trim  = function(str) { return str.toString().trim();        }
String.LTrim = function(str) { return str.toString().trim("left");  }
String.RTrim = function(str) { return str.toString().trim("right"); }

// These are for VBScript-feeling
function Trim(str)  { return str.toString().trim(); }
function LTrim(str) { return str.toString().trim("left"); }
function RTrim(str) { return str.toString().trim("right"); }

/Lunna

Reply via email to