String, array method combat--charat (), Split (), indexOf (), substring ()

Source: Internet
Author: User

This essay is based on two questions to combat some of the methods of arrays and strings.

Question one: Find out the most occurrences of the number of characters in a string

1 varstr = ' Fuuhuhuhufaihuhfnkjnkcnio ';?2     functionnum (str) {3         varJSON = {};4          for(vari = 0; i < str.length; i++){5             //the Charat () method of the string returns the string at the specified position6             if(!json[str.charat (i)]) {//assigns a value of 1 to the current property if there is no current property in the JSON object7Json[str.charat (i)] = 1;8}Else{9Json[str.charat (i)]++;//if the current property is already present in the JSON object, the current property value +1Ten             } One         } A         varIMax = 0; -         varIIndex = ' '; -          for(varIinchJSON) {//for in iterates over the properties of a JSON object the             if(json[i]>IMax) { -IMax =Json[i]; -IIndex =i; -             } +         } -Console.log (' Most occurrences are: ' +iindex+ ' appears ' +imax+ '); +     } ANum (str);

Question two: Output the value of the corresponding string

Specific description: Given a URL string, such as var url="http://www.baidu.com/?username=xiaohua&password=262763"; When we enter username, the value of the output username

Method One

1  varUrl= "http://www.baidu.com/?username=xiaohua&password=262763";2     functionUrle (key) {3         varValue= "";4         varCC = Url.split ('? ') [1].split (' & '));5          for(vari=0;i<cc.length;i++){6             varR =Cc[i];7             varA = r.split (' = '));8             if(key==a[0]){9Value = a[1];Ten                  Break; One             } A         } -         returnvalue; -     } theConsole.log (Urle (' username '));//Xiaohua

Url.split ('? ') Convert the URL string to an array: ["http://www.baidu.com/", "username=xiaohua&password=262763"], now the original string '? ' The following string is extracted and then converted to the array code, such as line fourth

Method Two

1 functionUrle (key) {2         varValue = "";3         varAA = Url.indexof ('? '));4         varbb = url.substring (aa+1);5         varCC = Bb.split (' & '));6          for(vari=0;i<cc.length;i++){7             varR =Cc[i];8             varA = r.split (' = '));9             if(key==a[0]){TenValue = a[1]; One                  Break; A             } -         } -         returnvalue; the     } -Console.log (Urle (' username '));//Xiaohua

String, array method combat--charat (), Split (), indexOf (), substring ()

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.