Multiple methods for deleting string spaces in javaScript _ javascript tips

Source: Internet
Author: User
This article introduces the function of deleting strings and using regular expressions to quickly replace all spaces in strings. The Code is as follows:


// Remove the leading space (left space) of the string)
Function LTrim (str ){
Var I;
For (I = 0; I If (str. charAt (I )! = "") Break;
}
Str = str. substring (I, str. length );
Return str;
}
// Remove the trailing space (right space) of the string)
Function RTrim (str ){
Var I;
For (I = str. length-1; I> = 0; I --){
If (str. charAt (I )! = "") Break;
}
Str = str. substring (0, I + 1 );
Return str;
}
// Remove the leading and trailing spaces (left and right spaces) of the string)
Function Trim (str ){
Return LTrim (RTrim (str ));
}


Delete all functions in the string
Js: delete string space function

The Code is as follows:


Function Jtrim (str)
{
Var I = 0;
Var len = str. length;
If (str = "") return (str );
J = len-1;
Flagbegin = true;
Flagend = true;
While (flagbegin = true) & (I <len ))
{
If (str. charAt (I) = "")
{
I = I + 1;
Flagbegin = true;
}
Else
{
Flagbegin = false;
}
}
While (flagend = true) & (j> = 0 ))
{
If (str. charAt (j) = "")
{
J = J-1;
Flagend = true;
}
Else
{
Flagend = false;
}
}
If (I> j) return ("");
Trimstr = str. substring (I, j + 1 );
Return trimstr;
}


The above method does not use regular expressions. Below we will try it using regular expressions.
Regular Expression Replacement space

The Code is as follows:


// Remove spaces in the string
String. prototype. Trim = function (){
Return this. replace (/(^ s *) | (s * $)/g ,"");
}
// Remove spaces on the left of the string
String. prototype. LTrim = function (){
Return this. replace (/(^ s *)/g ,"");
}
// Remove spaces on the right of the string
String. prototype. RTrim = function (){
Return this. replace (/(s * $)/g ,"");
}


Delete all spaces

The Code is as follows:


Var s = "asd ddd bbb sss ";
Var reg =/s/g;
Var ss = s. replace (reg ,"");
Alert (ss );


Remove all spaces in the string (including intermediate spaces. Set the 2nd parameter to g)

The Code is as follows:


Function Trim (str, is_global)
{
Var result;
Result = str. replace (/(^ s +) | (s + $)/g ,"");
If (is_global.toLowerCase () = "g ")
Result = result. replace (/s/g ,"");
Return result;
}

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.