Add TRIM, LTRIM, RTRIM Method for String Objects in JavaScript
Using the Prototype property of each object in JavaScript We can add our own methods and properties to the built-in objects in JavaScript.
Here we use this property to add three methods for String objects: TRIM, LTRIM, RTRIM (the same function as the same name in VBScript)
String.prototype.trim = function ()
{
Return this.Replace (/ (^ / s *) | (/ s * $) / g, "");
}
String.Prototype.ltrim = function ()
{
Return this.Replace (/ (^ / s *) / g, "");
}
String.prototype.rtrim = function ()
{
Return this.Replace (/ S * $) / g, "");
}
How, simple, look at an example of use:
String.prototype.trim = function ()
{
Return this.Replace (/ (^ / s *) | (/ s * $) / g, "");
}
Var s = "Leading and trailing spaces";
WINDOW.Alert (" S.Length ") ")
s = S.trim ();
WINDOW.Alert (" S.Length ") ")
script>