Simple operation string

Source: Internet
Author: User

Use JavaScript to perform common string operations.

1. strings are loaded into arrays separated by commas. You can use the split () function.

var str=",,,,,,,,,,,abc,123,4cc,569,dfk,,,,,,,,";function goArray(str){    var arr=[];    var s="";    var j=0;    for(var i=0;i<str.length;i++){        if(str.charAt(i)!=‘,‘&&i!=str.length-1){            s+=str.charAt(i);        }        else if(i==str.length-1&&str.charAt(i)!=‘,‘){            s+=str.charAt(i);            arr[j]=s;        }        else{        if(s!=""){                arr[j]=s;        j++;        s="";        }        }    }    console.log(s);    console.log(arr);}console.log("abc");goArray(str);console.log(str);

 

2. concatenate the number in the array into a string and use the join () function.

var arr=[‘abc‘,‘123‘,‘jack‘,‘fuck‘,‘669‘];function goString(arr){    var str="";    for(var i=0;i<arr.length;i++){        if(i!=arr.length-1) str+=arr[i]+‘:‘;        else{str+=arr[i];}    }    console.log(str);}goString(arr);

 

3. concatenate strings.

var a="jack";var b="rose";function newStr(str1,str2){    var str=str1+str2;    console.log(str);}newStr(a,b);

 

4. Replace the string using the Replace () function.

var str="Guys,people will not forget you. People loves you.";function replaceStr(str1,str2,str){    var num=0;var ss="";    for(var i=0;i<str.length;i++){        if(str.charAt(i)==str1.charAt(0)){            for(var s=0;s<str1.length-1;s++){               if(str.charAt(i+s+1)==str1.charAt(s+1)){                   num++;                   console.log(" num = "+num);               }             }                  if(num==str1.length-1){               num=0;               ss+=str2;               i+=str1.length-1;               console.log("when num==5,num=  "+num);               console.log("when num==5,s=  "+ss);               }               else                ss+=str.charAt(i);               num=0;        }        else            ss+=str.charAt(i);            console.log("when i= "+i+" , s=  "+ss);    }    console.log(ss);}replaceStr(‘people‘,‘jack‘,str);console.log(str.replace(/people/g,‘jack‘));

 

5. delete some strings and use substring.

var str="YOUAREJUSTASHITYOUKNOW?";function cutStr(str1,str){    var ss="";     var num=0;    for(var i=0;i<str.length;i++){        if(str.charAt(i)==str1.charAt(0)){            for(var j=0;j<str1.length-1;j++){                if(str.charAt(i+j+1)==str1.charAt(j+1)){num++;}            }            if(num==str1.length-1){                i+=str1.length-1;            }else{                ss+=str.charAt(i);            }            num=0;        }else{            ss+=str.charAt(i);        }    }    console.log(ss);}cutStr(‘YOU‘,str);console.log(str);

 

6. A small question: add any number with a comma to make it conform to the expression of the amount of money: 699,998 or 8,999.

Solution 1:

var money=‘9899999‘;var moon="";function getMoney(money){    if(money.length<4){    moon=money;    return;    }    for(var i=0;i<money.length;i++){        if(i<money.length%3){            moon+=money[i];            console.log(‘moon=  ‘+moon);            if(i==money.length%3-1)       moon+=‘,‘;         }else{       if((i-money.length%3)%3==0&&i!=money.length%3)           moon+=‘,‘;       moon+=money[i];        }    }}getMoney(money);console.log(moon);

 

Solution 2:

var str=‘208299‘;var sin=‘‘;for(var i=n=str.length; i>0; sin=((n-i--)%3==2&&i!=0?‘,‘+str[i]:str[i])+sin);console.log(sin);

 

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.