Below is the Trim function that can be used to trim the white space from a string.
// Trim function in javascript
function Trim(str)
{
while (str.substring(0,1) == ' ') // check for white spaces from beginning
{
str = str.substring(1, str.length);
}
while (str.substring(str.length-1, str.length) == ' ') // check white space from end
{
str = str.substring(0,str.length-1);
}
return str;
}
You can use this function like below
var str = " sampe ";
str = Trim(str);
The value of result will be "sample"