Blog Image

trim function in javascript

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;
}



Author: admin

Vinod Ram has been in Software Industry since 2006 and has experience of over 16 years in Software Development & Project Management domain specialised majorly in LAMP stack & Open Source Technology, building enterprise level Web based Application, Large Database driven and huge traffic Websites and Project Management. He loves to write information articles and blog to share his knowledge and experience with the outside world and help people to find solution for their problems.