Js remove space instance Trim () LTrim () RTrim () _ javascript tips-js tutorial

Source: Internet
Author: User
Js remove space instance Trim (), LTrim (), RTrim () need friends can come to refer to, hope to help everyone 1. Remove string spaces in js
// Remove the left space;
Function ltrim (s)
...{

Return s. replace (/(^ s *)/g ,"");
}
// Remove the right space;
Function rtrim (s)
...{
Return s. replace (/(s * $)/g ,"");
}
// Remove left and right spaces;
Function trim (s )...{
// S. replace (/(^ s *) | (s * $)/g ,"");
Return rtrim (ltrim (s ));

}

2. Remove spaces on both sides of the string.
// Parameter: string passed in by mystr
// Return: String mystr
Function trim (mystr ){
While (mystr. indexOf ("") = 0) & (mystr. length> 1 )){
Mystr = mystr. substring (1, mystr. length );
} // Remove the leading space
While (mystr. lastIndexOf ("") = mystr. length-1) & (mystr. length> 1 )){
Mystr = mystr. substring (0, mystr. length-1 );
} // Remove trailing Spaces
If (mystr = ""){
Mystr = "";
}
Return mystr;
}

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.