New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[js] 第7天 统计某一字符或字符串在另一个字符串中出现的次数 #21
Comments
function strCount(str, target) {
let count = 0
if (!target) return count
while(str.match(target)) {
str = str.replace(target, '')
count++
}
return count
}
console.log(strCount('abcdef abcdef a', 'abc')) |
function substrCount(str, target) {
let count = 0;
while (str.includes(target)) {
const index = str.indexOf(target);
count++;
str = str.substring(index + target.length);
}
return count;
} |
这里找到一个好方法。希望能有用。 |
function getStrTimes(str,rule){ return rule !=='' && str.match(new RegExp(rule,'g'))? str.match(new RegExp(rule,'g')).length : 0; } |
var childInNums = parent.split(child).length - 1, 这个没毛病吧 |
感觉没毛病 |
fucntion repeat(str,parentStr){
return parentStr.split(str).length - 1
} |
// 递归,一行代码实现
} |
|
const countAppears = (str, target) => {
let count = 0;
if (!str || !target) {
return count;
}
const keyIndex = target.indexOf(str);
if (keyIndex > -1) {
count = 1 + countAppears(str, target.slice(keyIndex + 1));
}
return count;
};
const str = "abcaaadefg2333333333334abcddddea";
console.log(countAppears("2", str));
console.log(countAppears("b", str));
console.log(countAppears("d", str));
console.log(countAppears("a", str));
console.log(countAppears("f", str)); |
这个好啊 又简单 |
function strFind (str, target) { |
这个真心不错 |
const countString = (str, chars) => (
(arr = str.match(new RegExp(chars, 'g'))), arr && chars ? arr.length : 0
) |
function 关键字写错了 |
|
function getTargetCount(arr, target) {
return arr
.reduce((group, v) => group.set(v, (group.get(v) || 0) + 1), new Map())
.get(target);
}
console.log(getTargetCount([1,1,2,5],1));// 2 |
|
哈哈哈哈和你的想法一样 |
const letter = "a";
const str = "abcaabccabcaa"
const time = (str, letter) => str.split(letter).length - 1;
console.log(time(str, letter)) // 6 不知道会不会有没有其他特殊情况,这样写的话。。。 |
function strCount(str, target) {
let count = 0;
while (str.includes(target)) {
str = str.replace(target, "");
count++;
}
return count;
}
function strCount(str, target) {
return str.split(target).length - 1;
}
console.log(strCount("asdgqwidugasgdakisdggfjwdagasd", "gas")); |
function count(str, string) {
return Math.max(0, string.split(str).length - 1);
} |
‘’aa‘’在“aaabcaaa”中出现几次?如果是两次上述答案没有问题,如果是四次 function subCount(str,target){
let matchs = str.match(new RegExp(`(?=${target})`, 'g'))
return matchs ? matchs.length : 0
} |
|
const subStrTimes = (str, sub) => {
return Array.from(str.matchAll(sub), m => m[0]).length;
} |
function getTimes(str1, str2) { |
var str = 'aabbccabcaabbccabcabc15114816181abc64641414tregfagfczfavacabaaabc6486';
|
function counter(str, subStr, num = 0, index = 0) {
let tempIndex = str.indexOf(subStr, index)
return tempIndex > -1 ? counter(str, subStr, ++num, ++tempIndex) : num
} |
|
|
|
let index = 0; |
...想复杂了 |
|
let counter = 0;//计数器 |
function frequency(str1,str2){ |
let num = 0; |
const countAppears = (char, str) => {
}; const str = "ab2dx22d"; console.log(countAppears("d", str)); |
|
function count(son, mother) { console.log(count('abc', 'abcdef abcdef a')); |
function toCase(str,target){ |
哈哈 我也是这么想的 |
split方法// split 方法
function countStr(str1, str2) {
return str2.split(str1).length - 1;
}
console.log(countStr("da", "dabshaudaoiddda")); includes + indexOf + substring 方法// includes 方法
function countStr(str1, str2) {
let count = 0;
while (str1 && str2 && str2.includes(str1)) {
let index = str2.indexOf(str1);
count++;
str2 = str2.substring(index + str1.length);
}
return count;
}
console.log(countStr("da", "dabshaudaoiddda")); match + replace 方法// match 方法
function countStr(str1, str2) {
let count = 0;
while (str1 && str2 && str2.match(str1)) {
str2 = str2.replace(str1, "");
count++;
}
return count;
}
console.log(countStr(" ", " ")); 递归// 递归
function countStr(str1, str2, count = 0) {
if (str1 && str2) {
return str2.indexOf(str1) !== -1
? countStr(
str1,
str2.substring(str2.indexOf(str1) + str1.length),
++count
)
: count;
}
return count;
}
console.log(countStr("da", "dabshaudaoiddda"));
console.log(countStr("", "")); |
|
我试了几个方法,这个的速度也是最快的 |
function GetCounts(srcData, DetectStr){ |
let str = 's' |
function strCount(str, target) { |
不使用正则: const strCountOf = (str = '', fromStr = '') => {
let count = 0;
let start = false;
let startId;
for (let i = 0; i < fromStr.length; i++) {
if (str[0] === fromStr[i]) {
start = true;
startId = i;
}
if (start) {
if (str[i - startId] !== fromStr[i]) {
start = false;
}
if (i === startId + str.length - 1) {
count++;
start = false;
}
}
}
return count;
} 使用正则: const strCountOf = (str = '', fromStr = '') => [...fromStr.matchAll(new RegExp(str, 'g'))].length; |
const countString = (childStr,parentStr) => parentStr.split(childStr).length - 1); |
function statCharInString(str, char) {
let re = new RegExp(char, 'g')
let num = str.match(re).length
console.log(num);
}
statCharInString('asf sfas sadf jjsfahf', 'as') |
// 统计某一字符或字符串在另一个字符串中出现的次数 |
|
function count(str, target) { count('a', 'abcsadfwretaabcerwawabc') |
function countStr(str, target){ |
function getCount(str, char) {
let index = str.indexOf(char);
if (index < 0) return 0;
return 1 + getCount(str.slice(index + char.length), char);
} |
function countStr(target, array, rightIndex, leftIndex, count) {
} let target = "aa"; 双指针 |
function countStr(str1, str2) { |
第7天 统计某一字符或字符串在另一个字符串中出现的次数
The text was updated successfully, but these errors were encountered: