1 var str = "aaddaabbcdddefg" 2 console.log(str.charAt(7));//b 没有返回空不是null 3 console.log(str.indexOf('p'));//1 没有返回-1 4 5 6 var obj = {}; 7 for(var i=0;i<str.length;i++){ 8 var v = str.charAt(i); 9 if(obj[v] && obj[v].value == v){ 10 obj[v].count ++ ; 11 }else{ 12 obj[v] = {}; 13 obj[v].count = 1; 14 obj[v].value = v; 15 } 16 } 17 18 console.log(obj);//true 19 //obj = {a:object,b:object,c:object} 20 for(key in obj){ 21 document.write(obj[key].value +'='+obj[key].count+' '); // a=4 b=3 c=4 d=2 f=1 g=1 h=1 22 } 23 24 25
记录字符串中每一项,并且记录个数