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 ()