Several Methods in js to remove spaces between the left and right strings

Source: Internet
Author: User
Tags rtrim

// 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 ,"");
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.