JavaScript trim() function
This is a function for removing leading and trailing spaces
from a string in JavaScript. For some reason, JavaScript doesn't
come with this function even though it is available in other
languages including Java and VBScript. The code below is ported from the trim function in Java.
// Trims leading and trailing spaces from a string
function trim(str) {
var len = str.length
var start = 0;
while ((start < len) && (str.charAt(start) == ' '))
start++;
while ((start < len) && (str.charAt(len-1) == ' '))
len--;
return ((start > 0) || (len < str.length)) ? str.substring(start, len): str;
}
|