substr_count
substr_count(string $haystack, string $needle, int $offset = 0, ?int $length = null): int
统计子串在字符串中出现的次数(非重叠计数),可用 offset/length 限定统计窗口。
C C23
#include <stdio.h>
#include <string.h>
/* 手写 strstr 循环:跳过整段命中 → 不计重叠;offset/length 用指针+截断模拟 */
static int substr_count(const char *haystack, size_t offset, size_t length, const char *needle) {
char buf[64];
size_t n = strlen(haystack);
if (offset > n) return 0;
if (length > n - offset) length = n - offset;
memcpy(buf, haystack + offset, length);
buf[length] = '\0';
int count = 0;
const char *p = strstr(buf, needle);
while (p != NULL) {
count++;
/* 从命中末尾继续搜索 → 不计重叠 */
p = strstr(p + strlen(needle), needle);
}
return count;
}
int main(void) {
printf("%d\n", substr_count("hello world hello", 0, 64, "hello")); /* 2 */
printf("%d\n", substr_count("aaaa", 0, 64, "aa")); /* 2:不计重叠 */
printf("%d\n", substr_count("abcabcabc", 3, 64, "abc")); /* offset 3 起 → 2 */
printf("%d\n", substr_count("abcabcabc", 0, 6, "abc")); /* 只看前 6 字节 → 2 */
return 0;
}
2 2 2 2
C++ C++26
#include <iostream>
#include <string>
// 手写 find 循环:跳过整段命中 → 不计重叠;offset/length 用 substr 模拟
static int substr_count(const std::string &haystack, size_t offset, size_t length, const std::string &needle) {
std::string region = haystack.substr(offset, length);
int count = 0;
// 从命中末尾继续搜索 → 不计重叠
for (size_t pos = region.find(needle); pos != std::string::npos;
pos = region.find(needle, pos + needle.size())) {
count++;
}
return count;
}
int main() {
std::cout << substr_count("hello world hello", 0, 64, "hello") << "\n"; // 2
std::cout << substr_count("aaaa", 0, 64, "aa") << "\n"; // 2:不计重叠
std::cout << substr_count("abcabcabc", 3, 64, "abc") << "\n"; // offset 3 起 → 2
std::cout << substr_count("abcabcabc", 0, 6, "abc") << "\n"; // 只看前 6 字节 → 2
return 0;
}
2 2 2 2
C# v11.0.100
// 无内置 count:手写 IndexOf 循环(跳过整段命中 → 不计重叠);offset/length 用区间模拟
static int SubstrCount(string haystack, string needle, int offset, int length)
{
int end = (int)Math.Min((long)offset + length, haystack.Length);
string region = haystack[offset..end];
int count = 0, pos = region.IndexOf(needle, StringComparison.Ordinal);
while (pos != -1)
{
count++;
// 从命中末尾继续搜索 → 不计重叠
pos = region.IndexOf(needle, pos + needle.Length, StringComparison.Ordinal);
}
return count;
}
Console.WriteLine(SubstrCount("hello world hello", "hello", 0, int.MaxValue)); // 2
Console.WriteLine(SubstrCount("aaaa", "aa", 0, int.MaxValue)); // 2:不计重叠
Console.WriteLine(SubstrCount("abcabcabc", "abc", 3, int.MaxValue)); // offset 3 起 → 2
Console.WriteLine(SubstrCount("abcabcabc", "abc", 0, 6)); // 只看前 6 字符 → 2
2 2 2 2
Go v1.27.4
package main
import (
"fmt"
"strings"
)
func main() {
// strings.Count 语义与 substr_count 一致——非重叠计数;offset/length 用切片模拟
fmt.Println(strings.Count("hello world hello", "hello")) // 2
fmt.Println(strings.Count("aaaa", "aa")) // 2:不计重叠
fmt.Println(strings.Count("abcabcabc"[3:], "abc")) // offset 3 起 → 2
fmt.Println(strings.Count("abcabcabc"[:6], "abc")) // 只看前 6 字节 → 2
}
2 2 2 2
Java v27.0.1
public class Main {
// 无内置 count:手写 indexOf 循环(跳过整段命中 → 不计重叠);offset/length 用窗口模拟
static int substrCount(String haystack, String needle, int offset, int length) {
int end = (int) Math.min((long) offset + length, haystack.length());
String region = haystack.substring(offset, end);
int count = 0, pos = region.indexOf(needle);
while (pos != -1) {
count++;
// 从命中末尾继续搜索 → 不计重叠
pos = region.indexOf(needle, pos + needle.length());
}
return count;
}
public static void main(String[] args) {
System.out.println(substrCount("hello world hello", "hello", 0, Integer.MAX_VALUE)); // 2
System.out.println(substrCount("aaaa", "aa", 0, Integer.MAX_VALUE)); // 2:不计重叠
System.out.println(substrCount("abcabcabc", "abc", 3, Integer.MAX_VALUE)); // offset 3 起 → 2
System.out.println(substrCount("abcabcabc", "abc", 0, 6)); // 只看前 6 字符 → 2
}
}
2 2 2 2
JavaScript v26.13.1
// 无内置 count:手写 indexOf 循环(从命中末尾继续 → 不计重叠);slice 自动收尾夹取
function substrCount(haystack, needle, offset = 0, length = Infinity) {
const region = haystack.slice(offset, offset + length);
let count = 0, pos = region.indexOf(needle);
while (pos !== -1) {
count++;
// 跳过整段命中 → 不计重叠
pos = region.indexOf(needle, pos + needle.length);
}
return count;
}
console.log(substrCount("hello world hello", "hello")); // 2
console.log(substrCount("aaaa", "aa")); // 2:不计重叠
console.log(substrCount("abcabcabc", "abc", 3)); // offset 3 起 → 2
console.log(substrCount("abcabcabc", "abc", 0, 6)); // 只看前 6 字符 → 2
2 2 2 2
PHP v8.6.0
// substr_count:内置子串计数——**不计重叠**(strpos 全家的配套统计口)
echo substr_count("hello world hello", "hello"), "\n"; // 2
echo substr_count("aaaa", "aa"), "\n"; // 2:位置 0/2,重叠位置 1 不计
echo substr_count("abcabcabc", "abc", 3), "\n"; // offset 3 起算 → 2
echo substr_count("abcabcabc", "abc", 0, 6), "\n"; // 只看前 6 字节 → 2
2 2 2 2
Python v3.15.2
# str.count 语义与 substr_count 一致——非重叠;start/end 原生对应 offset/length
print("hello world hello".count("hello")) # 2
print("aaaa".count("aa")) # 2:Python 也不计重叠
print("abcabcabc".count("abc", 3)) # start 3 起 → 2
print("abcabcabc".count("abc", 0, 6)) # 只看前 6 字符 → 2
2 2 2 2
Rust v1.99.1
fn main() {
// str::matches 迭代器天然非重叠;offset/length 用切片模拟
println!("{}", "hello world hello".matches("hello").count()); // 2
println!("{}", "aaaa".matches("aa").count()); // 2:不计重叠
println!("{}", "abcabcabc"[3..].matches("abc").count()); // offset 3 起 → 2
println!("{}", "abcabcabc"[..6].matches("abc").count()); // 只看前 6 字节 → 2
}
2 2 2 2
TypeScript v26.13.1
// 无内置 count:手写 indexOf 循环(从命中末尾继续 → 不计重叠);slice 自动收尾夹取
function substrCount(haystack: string, needle: string, offset = 0, length = Infinity): number {
const region = haystack.slice(offset, offset + length);
let count = 0, pos = region.indexOf(needle);
while (pos !== -1) {
count++;
// 跳过整段命中 → 不计重叠
pos = region.indexOf(needle, pos + needle.length);
}
return count;
}
console.log(substrCount("hello world hello", "hello")); // 2
console.log(substrCount("aaaa", "aa")); // 2:不计重叠
console.log(substrCount("abcabcabc", "abc", 3)); // offset 3 起 → 2
console.log(substrCount("abcabcabc", "abc", 0, 6)); // 只看前 6 字符 → 2
2 2 2 2
语言差异说明
重叠计数语义是本条目核心:各家一律不计重叠——substr_count("aaaa","aa") 是 2 不是 3;要重叠计数得自己手写(逐位置 +1 后前进 1)。
C:无内置——手写 strstr 循环(示例);关键在从命中末尾继续搜索(p + strlen(needle))。
C++:同思路 string::find 循环(示例);ranges 无现成 count。
C#: 无内置 count——手写 IndexOf 循环(示例);LINQ 也无现成物。
Go:strings.Count 语义与 PHP 一致(非重叠);offset/length 用切片 s[3:]/s[:6] 模拟。
Java:无内置 count——手写 indexOf 循环(示例);Apache Commons Lang 的 StringUtils.countMatches 同样非重叠。
JavaScript:无内置——手写 indexOf 循环(示例);正则全局匹配 s.match(/aa/g)?.length 也天然非重叠。
PHP:substr_count 内置——$offset/$length 直接切统计窗口(越界返回 false/警告);不计重叠;与 strpos 全家(strpos/strrpos/stripos)配对使用。
Python:str.count 语义与 PHP 一致(非重叠),且 start/end 参数原生对应 offset/length——十门语言里迁移成本最低。
Rust:str::matches().count() 天然非重叠;offset/length 用切片 s[3..]/s[..6] 模拟(越界 panic 要自查)。
TypeScript:同 JavaScript(手写循环或正则全局匹配)。