| 标题 | 标签 |
|---|---|
| countSubstrings(计算字符串中子字符串出现的次数) | string,algorithm,beginner(字符串,算法,初学者) |
计算给定字符串中子字符串的出现次数。
- 使用
Array.prototype.indexOf()在str中查找searchValue。 - 如果找到值并更新索引,则增加一个计数器,即 i。
- 使用一个
while循环,该循环将在Array.prototype.indexOf()返回的值为-1时立即返回。
代码如下:
const countSubstrings = (str, searchValue) => {
let count = 0,
i = 0;
while (true) {
const r = str.indexOf(searchValue, i);
if (r !== -1) {
[count, i] = [count + 1, r + 1];
} else {
return count;
}
}
};ts 代码如下:
调用方式:
countSubstrings('tiktok tok tok tik tok tik', 'tik'); // 3
countSubstrings('tutut tut tut', 'tut'); // 4应用场景
以下是一个实战示例:
结果如下:
<iframe src="codes/javascript/html/count-substrings.html"></iframe>