我需要计算字符串中某个字符出现的次数。
例如,假设我的字符串包含:
var mainStr = "str1,str2,str3,str4";
我想求出逗号的个数,也就是3个字符。以及按逗号分隔后的单个字符串的计数,也就是4。
我还需要验证每个字符串,即str1或str2或str3或str4不应该超过,比如说,15个字符。
我需要计算字符串中某个字符出现的次数。
例如,假设我的字符串包含:
var mainStr = "str1,str2,str3,str4";
我想求出逗号的个数,也就是3个字符。以及按逗号分隔后的单个字符串的计数,也就是4。
我还需要验证每个字符串,即str1或str2或str3或str4不应该超过,比如说,15个字符。
当前回答
该函数以字符串str作为参数,并计算字符串中每个唯一字符的出现次数。结果是每个字符的键值对。
var charFoundMap = {};//object defined
for (var i = 0; i < str.length; i++) {
if(!charFoundMap[ str[i] ]) {
charFoundMap[ str[i] ]=1;
}
else
charFoundMap[ str[i] ] +=1;
//if object does not contain this
}
return charFoundMap;
}
其他回答
下面是一个类似的解决方案,但它使用了Array.prototype.reduce
function countCharacters(char, string) {
return string.split('').reduce((acc, ch) => ch === char ? acc + 1: acc, 0)
}
如前所述,String.prototype.split的工作速度比String.prototype.replace快得多。
我使用的是Node.js v.6.0.0,最快的是带索引的(Lo Sauer回答中的第三个方法)。
二是:
函数count(s, c) { Var n = 0; 对于(令x (s)) { 如果(x == c) n + +; } 返回n; }
至少有五种方法。最好的选项,也应该是最快的(由于本机RegEx引擎)被放在顶部。
方法1
("this is foo bar".match(/o/g)||[]).length;
// returns 2
方法2
"this is foo bar".split("o").length - 1;
// returns 2
不建议拆分,因为它是资源饥渴的。它为每个匹配分配新的“Array”实例。不要通过FileReader尝试>100MB文件。你可以观察确切的资源使用使用Chrome的分析器选项。
方法3
var stringsearch = "o"
,str = "this is foo bar";
for(var count=-1,index=-2; index != -1; count++,index=str.indexOf(stringsearch,index+1) );
// returns 2
方法4
搜索单个字符
var stringsearch = "o"
,str = "this is foo bar";
for(var i=count=0; i<str.length; count+=+(stringsearch===str[i++]));
// returns 2
方法5
元素映射和过滤。不建议这样做,因为它的整体资源预分配,而不是使用python的“生成器”:
var str = "this is foo bar"
str.split('').map( function(e,i){ if(e === 'o') return i;} )
.filter(Boolean)
//>[9, 10]
[9, 10].length
// returns 2
分享: 我做了这个要点,目前有8种方法的字符计数,所以我们可以直接汇集和分享我们的想法-只是为了好玩,也许一些有趣的基准:)
我更新了这个答案。我更喜欢使用火柴的想法,但它更慢:
console.log((“str1,str2,str3,str4”.match(/,/g) ||[]).长度);日志 3 console.log((“str1,str2,str3,str4”.match(new RegExp(“str”, “g”)) ||[]).长度);日志 4
如果事先知道要搜索什么,可以使用正则表达式文字;如果不知道,可以使用RegExp构造函数,并传入g标志作为参数。
匹配结果为空,因此|| []
以下是我在2009年给出的原始答案。它创建了一个不必要的数组,但是使用分割更快(截至2014年9月)。我很矛盾,如果我真的需要速度,毫无疑问我会使用分拆,但我更喜欢使用匹配。
旧答案(2009年):
如果你在找逗号:
(mainStr.split(",").length - 1) //3
如果你在找str
(mainStr.split("str").length - 1) //4
在@Lo的回答和我自己的愚蠢的性能测试中,分裂在速度上领先,至少在Chrome中,但再次创建额外的数组似乎不理智。
我刚刚在repl上做了一个快速而肮脏的测试。它使用Node v7.4。对于单个字符,标准的For循环是最快的:
一些代码:
// winner!
function charCount1(s, c) {
let count = 0;
c = c.charAt(0); // we save some time here
for(let i = 0; i < s.length; ++i) {
if(c === s.charAt(i)) {
++count;
}
}
return count;
}
function charCount2(s, c) {
return (s.match(new RegExp(c[0], 'g')) || []).length;
}
function charCount3(s, c) {
let count = 0;
for(ch of s) {
if(c === ch) {
++count;
}
}
return count;
}
function perfIt() {
const s = 'Hello, World!';
const c = 'o';
console.time('charCount1');
for(let i = 0; i < 10000; i++) {
charCount1(s, c);
}
console.timeEnd('charCount1');
console.time('charCount2');
for(let i = 0; i < 10000; i++) {
charCount2(s, c);
}
console.timeEnd('charCount2');
console.time('charCount3');
for(let i = 0; i < 10000; i++) {
charCount2(s, c);
}
console.timeEnd('charCount3');
}
几次运行的结果:
perfIt()
charCount1: 3.301ms
charCount2: 11.652ms
charCount3: 174.043ms
undefined
perfIt()
charCount1: 2.110ms
charCount2: 11.931ms
charCount3: 177.743ms
undefined
perfIt()
charCount1: 2.074ms
charCount2: 11.738ms
charCount3: 152.611ms
undefined
perfIt()
charCount1: 2.076ms
charCount2: 11.685ms
charCount3: 154.757ms
undefined
更新2021年2月10日:修复了repl中的拼写错误。它演示
更新2020年10月24日:Node.js 12仍然是这样(你自己在这里玩)