Use JS to remove the front and back space or the middle space Daquan

Source: Internet
Author: User
Tags rtrim

1. remove all spaces before and after the string :

--JS to achieve the trim function

Remove all empty strings before and after the string
function Trim (str) { return str.replace (/(^\s*) | ( \s*$)/g, ""); }
Adding a method to a string prototype can also
String.prototype.Trim = function ()
{
Return This.replace (/(^\s*) | ( \s*$)/g, "");
}

Remove Space before string
function () { returnthis. Replace (/(^\s*)/g, "");}
Remove space after string
function () {    returnthis. Replace (/(\s*$)/g, "");}

Description:

If you use jquery to directly use the $. Trim(str) method, str indicates that you want to remove all the spaces before and after the string.

--function to remove whitespace on both sides of a string

Parameter: MyStr passed in string
Return: String mystr

// Remove Front spaces function Trim (mystr) {   while ((Mystr.indexof (") ==0) && (mystr.length>1 )) {    mystr =mystr.substring (1, mystr.length);   }

Remove trailing spaces
while ((Mystr.lastindexof (") ==mystr.length-1) && (mystr.length>1)) {
Mystr=mystr.substring (0,mystr.length-1);
}

  if (mystr== "") {
Mystr= "";
}
return mystr;
}
str = str.replace (/[]/g, "");//Replace all blanks//Erase All BlanksfunctionAllTrim (s) {varS1 =LTrim (s); return(RTrim (S1));}//Delete left blank (with Fullwidth blank)functionLTrim (s) {s= "" +s; varLen =s.length; varpos = 0; varS1 = "";  while(Pos <Len) {        varCHR = s.substring (pos, pos + 1); if(Chr = = "" | | CHR = = "" | | CHR = = "") Pos= pos + 1; Else            return(s.substring (Pos,len)); }    returnS1;}//Remove right margin (with Fullwidth Blanks)functionRTrim (s) {s= "" +s; varLen =s.length; varpos = len-1; varS1 = ""; vari = 0;  while(POS >= 0)    {        varCHR = s.substring (pos, s.length-i); if(Chr = = "" | | CHR = = "" | | CHR = = "") Pos= Pos-1; Else            return(s.substring (0, POS +1)); I++; }    returnS1;}//Remove extra space in the string (keep a blank)functionMiddletrim (str) {varRepstr= "";  for(vari=0;i<str.length;i++)    {        if(Str.substring (i,i+1)! = "") Repstr=repstr+str.substring (i,i+1); Else        {            if(Repstr.substring (repstr.length-1,repstr.length)! = "") {Repstr=repstr+ ""; }        }    }    returnrepstr;}

2, Remove all the spaces in the string (including the middle space, you need to set the 2nd parameter is: g)

function Trim (Str,is_global)        {            var  result;             = Str.replace (/(^\s+) | ( \s+$)/g, "");             if (is_global.tolowercase () = = "G")            {                = Result.replace (/\s/g, "");             }             return result;}

Use JS to remove the front and back space or the middle space Daquan

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.