当前位置:网站首页>软通乐学-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]+'次');
};
边栏推荐
- Installation and use of VMware Tools and open VM tools: solve the problems of incomplete screen and unable to transfer files of virtual machines
- Opencv learning log 28 -- detect the red cup cover
- Sword finger offer II 019 Delete at most one character to get a palindrome
- 2078. Two houses with different colors and the farthest distance
- Is the sanic asynchronous framework really so strong? Find truth in practice
- “鬼鬼祟祟的”新小行星将在本周安全掠过地球:如何观看
- What is the difficulty of programming?
- QT实现圆角窗口
- QT按钮点击切换QLineEdit焦点(含代码)
- 1005. Maximized array sum after K negations
猜你喜欢
Codeforces Round #801 (Div. 2)A~C
< li> dot style list style type
Remove the border when input is focused
Install Jupiter notebook under Anaconda
QWidget代码设置样式表探讨
Li Kou - 298th weekly match
Flag framework configures loguru logstore
Frida hook so layer, protobuf data analysis
Anaconda下安装Jupyter notebook
antd upload beforeUpload中禁止触发onchange
随机推荐
Basic Q & A of introductory C language
Suffix expression (greed + thinking)
Write web games in C language
Local visualization tools are connected to redis of Alibaba cloud CentOS server
Opencv learning log 27 -- chip positioning
Codeforces Round #802(Div. 2)A~D
Input can only input numbers, limited input
1323. Maximum number of 6 and 9
Problem - 922D、Robot Vacuum Cleaner - Codeforces
本地可视化工具连接阿里云centOS服务器的redis
JS call camera
Specify the format time, and fill in zero before the month and days
Flask框架配置loguru日志库
input 只能输入数字,限定输入
QT模拟鼠标事件,实现点击双击移动拖拽等
<li>圆点样式 list-style-type
Codeforces Round #799 (Div. 4)A~H
Installation and configuration of MariaDB
Opencv learning log 29 -- gamma correction
Problem - 1646C. Factorials and Powers of Two - Codeforces