JS 字符串

时间:2024-01-05 22:23:44
             var str = "aaddaabbcdddefg"
console.log(str.charAt());//b 没有返回空不是null
console.log(str.indexOf('p'));//1 没有返回-1 var obj = {};
for(var i=;i<str.length;i++){
var v = str.charAt(i);
if(obj[v] && obj[v].value == v){
obj[v].count ++ ;
}else{
obj[v] = {};
obj[v].count = ;
obj[v].value = v;
}
} console.log(obj);//true
//obj = {a:object,b:object,c:object}
for(key in obj){
document.write(obj[key].value +'='+obj[key].count+'&nbsp;'); // a=4 b=3 c=4 d=2 f=1 g=1 h=1
}

记录字符串中每一项,并且记录个数