function trim(sString) {
sTrimmedString = “”;
if (sString != “”) {
var iStart = 0;
var iEnd = sString.length – 1;
var sWhitespace = ” \t\f\n\r”;

while (sWhitespace.indexOf(sString.charAt(iStart)) != -1) {
iStart++;
if (iStart > iEnd)
break;
}

// If the string not just whitespace
if (iStart <= iEnd) {
while (sWhitespace.indexOf(sString.charAt(iEnd)) != -1)
iEnd–;
sTrimmedString = sString.substring(iStart,++iEnd);
}
}
return sTrimmedString;
}

trim function in javascript

Leave your comment