当前位置:网站首页>软通乐学-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]+'次');
};
边栏推荐
- 2027. Minimum number of operations to convert strings
- OneForAll安装使用
- Find 3-friendly Integers
- Programmers, what are your skills in code writing?
- D - function (HDU - 6546) girls' competition
- What is the difficulty of programming?
- C language must memorize code Encyclopedia
- (POJ - 2739) sum of constructive prime numbers (ruler or two points)
- Auto. Getting started with JS
- 875. Leetcode, a banana lover
猜你喜欢
QT implementation fillet window
Flask框架配置loguru日志庫
本地可视化工具连接阿里云centOS服务器的redis
7-1 understand everything (20 points)
QT实现窗口渐变消失QPropertyAnimation+进度条
window11 conda安装pytorch过程中遇到的一些问题
Codeforces Round #801 (Div. 2)A~C
921. Minimum additions to make parentheses valid
分享一个在树莓派运行dash应用的实例。
1605. Sum the feasible matrix for a given row and column
随机推荐
Codeforces Round #798 (Div. 2)A~D
Calculate the time difference
日期加1天
The "sneaky" new asteroid will pass the earth safely this week: how to watch it
1323. Maximum number of 6 and 9
QT style settings of qcobobox controls (rounded corners, drop-down boxes, up expansion, editable, internal layout, etc.)
Sword finger offer II 019 Delete at most one character to get a palindrome
Advancedinstaller安装包自定义操作打开文件
Ball Dropping
计算时间差
Truck History
C basic grammar
It is forbidden to trigger onchange in antd upload beforeupload
Kubernetes集群部署
Summary of FTP function implemented by qnetworkaccessmanager
图图的学习笔记-进程
Li Kou - 298th weekly match
Raspberry pie 4b64 bit system installation miniconda (it took a few days to finally solve it)
969. Pancake sorting
Codeforces - 1526C1&&C2 - Potions