// Recon ideas:
//-------------
// Remove the space on the left of the string
Function ltrim (str)
{
If (str. charat (0) = "")
{
// If the first character on the left of the string is a space
Str = str. slice (1); // remove spaces from the string
// This sentence can also be changed to str = str. substring (1, str. length );
Str = ltrim (str); // recursive call
}
Return str;
}
// Remove the spaces on the right of the string
Function rtrim (str)
{
Var ilength;
Ilength = str. length;
If (str. charat (ilength-1) = "")
{
// If the first character on the right of the string is a space
Str = str. slice (0, ilength-1); // remove spaces from the string
// This sentence can also be changed to str = str. substring (0, ilength-1 );
Str = rtrim (str); // recursive call
}
Return str;
}
// Remove the spaces on both sides of the string
Function trim (str)
{
Return ltrim (rtrim (str ));
}
// Train of thought for rainy day 5337:
//----------------
Function alltrim (a_strvarcontent)
{
Var pos1, pos2, newstring;
Pos1 = 0;
Pos2 = 0;
Newstring = ""
If (a_strvarcontent.length> 0)
{
For (I = 0; I <= a_strvarcontent.length; I ++)
// Recon: This sentence should have an error and should be changed:
// For (I = 0; I <a_strvarcontent.length; I ++)
{
If (a_strvarcontent.charat (I) = "")
Pos1 = pos1 + 1;
Else
Break;
}
For (I = a_strvarcontent.length; I> = 0; I --)
// Recon: This sentence should have an error and should be changed:
// For (I = a_strvarcontent.length-1; I> = 0; I --)
{
If (a_strvarcontent.charat (I) = "")
Pos2 = pos2 + 1;
Else
Break;
}
Newstring = a_strvarcontent.substring (pos1. a_strvarcontent.length-pos2)
}
Return newstring;
}
// Hoke's idea:
//-------------
Function jtrim (sstr)
{
Var astr = "";
Var dstr = "";
Var flag = 0;
For (I = 0; I <sstr. length; I ++)
{If (sstr. charat (I )! = '') | (Flag! = 0 ))
{Dstr + = sstr. charat (I );
Flag = 1;
}
}
Flag = 0;
For (I = dstr. length-1; I> = 0; I --)
{If (dstr. charat (I )! = '') | (Flag! = 0 ))
{Astr + = dstr. charat (I );
Flag = 1;
}
}
Dstr = "";
For (I = astr. length-1; I> = 0; I --) dstr + = astr. charat (I );
Return dstr;
}
Why not use a regular expression?
String. prototype. Trim = function ()
{
Return this. replace (/(^ \ s *) | (\ s * $)/g ,"");
}