RedBlueGreen SmallMediumLarge WideNarrowFluid
Your Cart 0 items,total ($0) View Cart
Javascript: trim, ltrim and rtrim in javascript PDF Print E-mail
User Rating: / 1
PoorBest 
Monday, 27 September 2010 19:24

Trim , Left trim (ltrim) and Right Trim (rtrim) in javascript

//Function to trim the space in the left side of the string
function ltrim ( s ){
return s.replace( /^s*/, "" );
}

//Function to trim the space in the right side of the string
function rtrim ( s ){
return s.replace( /s*$/, "" );
}

//*Function to trim the space in the  string
function trim(s) {
var temp = s;
return temp.replace(/^s+/,'').replace(/s+$/,'');

ltrim() will remove all the spaces to the left in a string rtrim() will remove all the spaces to the right in a string trim() will all the left , right and spaces inside the string Ex: var x = " thestring"; ltrim(x) will return “thestring"; var y = “thestring “; rtrim(y) will return “thestring"; var z = " the st ri ng “; trim(z) will return “thestring"