当前位置:网站首页>软通乐学-js求字符串中字符串当中那个字符出现的次数多 -冯浩的博客
软通乐学-js求字符串中字符串当中那个字符出现的次数多 -冯浩的博客
2022-07-06 09:29:00 【冯浩(grow up)】
使用的函数:charAt():根据位置取字符串
思路:我们通过循环将字符串通过charAt通过下标找到字符,
然后存储到一个对象对象名中,然后找到对象对应的属性值,
如果有这个对象名,就进行加一,
如果没有就设置为1
最后遍历这个对象,就可以得到了
var a = "123abcdefaaaaaaaaaaaaaaaaaaaa";
// 创建一个数组用来储存元素
var obj = new Object();
// 循环整个字符串的的长度
for(var i =0;i<a.length;i++){
// charAt:根据位置取字符串
// 循环去除字符串
var value = a.charAt(i);
console.log(value);
if(obj[value]>0){
obj[value]++;
}else{
obj[value]=1;
}
}
console.log(obj);
for(var j in obj){
console.log(j+"出现的次数"+obj[j]+'次');
};
边栏推荐
猜你喜欢
Codeforces Round #802(Div. 2)A~D
1529. Minimum number of suffix flips
Openwrt source code generation image
969. Pancake sorting
409. Longest palindrome
1605. Sum the feasible matrix for a given row and column
Codeforces Round #801 (Div. 2)A~C
The concept of C language array
Programmers, what are your skills in code writing?
Suffix expression (greed + thinking)
随机推荐
Bidirectional linked list - all operations
C basic grammar
2027. Minimum number of operations to convert strings
Educational Codeforces Round 130 (Rated for Div. 2)A~C
顺丰科技智慧物流校园技术挑战赛(无t4)
Ball Dropping
1855. Maximum distance of subscript alignment
Penetration test 2 --- XSS, CSRF, file upload, file inclusion, deserialization vulnerability
树莓派4B安装opencv3.4.0
Codeforces Round #800 (Div. 2)AC
Some problems encountered in installing pytorch in windows11 CONDA
Flag framework configures loguru logstore
C language learning notes
Remove the border when input is focused
Oneforall installation and use
628. Maximum product of three numbers
Sanic异步框架真的这么强吗?实践中找真理
去掉input聚焦时的边框
Basic Q & A of introductory C language
QT implementation fillet window