Javascript 5 ways to filter delete before and after all spaces

Source: Internet
Author: User
Tags rtrim

First: cyclic check replacement//for the user to invokefunctionTrim (s) {returnTrimRight (Trimleft (s));} //Remove the left blankfunctionTrimleft (s) {if(s = =NULL) {     return""; }   varwhitespace =NewString ("\t\n\r"); varstr =NewString (s); if(Whitespace.indexof (Str.charat (0))! =-1) {     varj=0, i =str.length;  while(J < i && Whitespace.indexof (Str.charat (j))! =-1) {J++; } STR=Str.substring (J, I); }   returnstr;} //Remove the blank www.jb51.net on the rightfunctionTrimRight (s) {if(s = =NULL)return""; varwhitespace =NewString ("\t\n\r"); varstr =NewString (s); if(Whitespace.indexof (Str.charat (str.length-1))! =-1){     vari = Str.length-1;  while(I >= 0 && whitespace.indexof (str.charat (i))! =-1) {i--; } STR= str.substring (0, i+1); }   returnstr;} 
the second type: regular replacement<script language= "JavaScript" > <!--String.prototype.Trim=function() { return  This. Replace (/(^\s*) | ( \s*$)/g, ""); } String.prototype.LTrim=function() { return  This. Replace (/(^\s*)/g, ""); } String.prototype.RTrim=function() { return  This. Replace (/(\s*$)/g, ""); } // -</SCRIPT>//go left space;functionLTrim (s) {returnS.replace (/(^\s*)/g, "");}//go to the right space;functionRTrim (s) {returnS.replace (/(\s*$)/g, "");}//to the left and right spaces;functionTrim (s) {returnS.replace (/(^\s*) | ( \s*$)/g, "");}
the third type: Using Jquery$.trim (str) jquery internal implementation is:functionTrim (str) {returnStr.replace (/^ (\s|\u00a0) +/, '). Replace (/(\S|\U00A0) +$/, '); } The fourth type: using MotoolsfunctionTrim (str) {returnStr.replace (/^ (\s|\xa0) +| ( \S|\XA0) +$/g, "); } Fifth: clipping string modefunctionTrim (str) {str= Str.replace (/^ (\s|\u00a0) +/, ");  for(varI=str.length-1; i>=0; i--){      if(/\s/. Test (Str.charat (i))) {STR= str.substring (0, i+1);  Break; }    }    returnstr; }




//   去掉字符串前后的空格
//  返回值:
//  去除空格后的字符串
//----------------------------------------------------------
function trim(param) {
   if ((vRet = param) == ‘‘ ) { return vRet; }
   while ( true ) {
     if (vRet.indexOf ( ‘ ‘ ) == 0) {
       vRet = vRet.substring(1, parseInt(vRet.length));
     } else if ((parseInt(vRet.length) != 0) && (vRet.lastIndexOf ( ‘ ‘ ) == parseInt(vRet.length) - 1)) {
       vRet = vRet.substring(0, parseInt(vRet.length) - 1);
     } else {
       return vRet;
     }
   }
}

Javascript 5 ways to filter delete before and after all spaces

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.