我需要计算一个字符在一个字符串中出现的次数。
例如,假设我的字符串包含:
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个字符。
I know this might be an old question but I have a simple solution for low-level beginners in JavaScript.
As a beginner, I could only understand some of the solutions to this question so I used two nested FOR loops to check each character against every other character in the string, incrementing a count variable for each character found that equals that character.
I created a new blank object where each property key is a character and the value is how many times each character appeared in the string(count).
Example function:-
function countAllCharacters(str) {
var obj = {};
if(str.length!==0){
for(i=0;i<str.length;i++){
var count = 0;
for(j=0;j<str.length;j++){
if(str[i] === str[j]){
count++;
}
}
if(!obj.hasOwnProperty(str[i])){
obj[str[i]] = count;
}
}
}
return obj;
}
The function takes string str as parameter and counts occurrence of each unique characters in the string. The result comes in key - value pair for each character.
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;
}
My solution:
function countOcurrences(str, value){
var regExp = new RegExp(value, "gi");
return str.match(regExp) ? str.match(regExp).length : 0;
}
And there is:
function character_count(string, char, ptr = 0, count = 0) {
while (ptr = string.indexOf(char, ptr) + 1) {count ++}
return count
}
Works with integers too!
我正在使用Node.js v.6.0.0,最快的是带有索引的索引(Lo Sauer的答案中的第三种方法)。
第二个是:
function count(s, c) {
var n = 0;
for (let x of s) {
if (x == c)
n++;
}
return n;
}
那么string.split(desiredCharecter).length-1
例:
var str =“让生活变得更美好”; var len = str.split(“ h”)。length-1; 将为上述字符串中的字符“ h”提供计数2;
我正在做一个需要子字符串计数器的小项目。搜索错误的短语给我没有任何结果,但是在编写自己的实现之后,我偶然发现了这个问题。无论如何,这是我的方式,它可能比这里的大多数慢,但可能对某人有所帮助:
function count_letters() {
var counter = 0;
for (var i = 0; i < input.length; i++) {
var index_of_sub = input.indexOf(input_letter, i);
if (index_of_sub > -1) {
counter++;
i = index_of_sub;
}
}
如果您发现此实现失败或不遵循某些标准,请告诉我!:)
更新 您可能想要替代:
for (var i = 0; i < input.length; i++) {
带有:
for (var i = 0, input_length = input.length; i < input_length; i++) {
有趣的阅读内容讨论了上述内容:http : //www.erichynds.com/blog/javascript-length-property-is-a-stored-value
我发现,搜索很大字符串(例如,长度为1 000 000个字符)中的字符的最佳方法是使用该replace()
方法。
window.count_replace = function (str, schar) {
return str.length - str.replace(RegExp(schar), '').length;
};
您可以看到另一个JSPerf套件来测试此方法以及其他在字符串中查找字符的方法。
这是一个类似的解决方案,但它使用 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
。
将此功能添加到sting原型:
String.prototype.count=function(c) {
var result = 0, i = 0;
for(i;i<this.length;i++)if(this[i]==c)result++;
return result;
};
用法:
console.log("strings".count("s")); //2
In javascript you can use above code to get occurrence of a character in a string.