strstr
strstr(string $haystack, string $needle, bool $before_needle = false): string|false
查找子串首次出现并返回子串本体:默认返回匹配处到末尾,before_needle=true 返回匹配之前部分;未命中返回 false。
C C23
C23
#include <stdio.h>
#include <string.h>
int main(void) {
const char *email = "name@example.com";
const char *p = strstr(email, "@"); // C 同名原祖:命中返指针、未命中返 NULL
if (p != NULL) {
printf("%s\n", p); // strstr($email, '@')
printf("%ld\n", (long)(p - email)); // 字节偏移——即 strpos() 的返回值
printf("%.*s\n", (int)(p - email), email); // strstr($email, '@', true)
}
printf("%d\n", strstr(email, "#") == NULL);
return 0;
}
输出
@example.com 4 name 1
C++ C++26
C++26
#include <iostream>
#include <string>
int main() {
std::string email = "name@example.com";
std::size_t pos = email.find('@'); // npos 表未命中,如同 PHP 的 false
if (pos != std::string::npos) {
std::cout << email.substr(pos) << "\n"; // strstr($email, '@')
std::cout << email.substr(0, pos) << "\n"; // strstr($email, '@', true)
}
std::cout << (email.find('#') == std::string::npos) << "\n";
return 0;
}
输出
@example.com name 1
C# v11.0.100
v11.0.100
string email = "name@example.com";
int pos = email.IndexOf('@'); // -1 表未命中,如同 PHP 的 false
Console.WriteLine(email.Substring(pos)); // strstr($email, '@')
Console.WriteLine(email.Substring(0, pos)); // strstr($email, '@', true)
Console.WriteLine(email.IndexOf('#') == -1);
输出
@example.com name True
Go v1.27.4
v1.27.4
package main
import (
"fmt"
"strings"
)
func main() {
email := "name@example.com"
pos := strings.Index(email, "@") // -1 表未命中,如同 PHP 的 false
if pos >= 0 {
fmt.Println(email[pos:]) // strstr($email, '@')
fmt.Println(email[:pos]) // strstr($email, '@', true)
}
fmt.Println(strings.Index(email, "#") < 0)
}
输出
@example.com name true
Java v27.0.1
v27.0.1
public class Main {
public static void main(String[] args) {
String email = "name@example.com";
int pos = email.indexOf('@'); // -1 表未命中,如同 PHP 的 false
System.out.println(email.substring(pos)); // strstr($email, '@')
System.out.println(email.substring(0, pos)); // strstr($email, '@', true)
System.out.println(email.indexOf('#') == -1);
}
}
输出
@example.com name true
JavaScript v26.13.1
v26.13.1
const email = "name@example.com";
const pos = email.indexOf("@"); // -1 表未命中,如同 PHP 的 false
console.log(email.slice(pos)); // strstr($email, '@')
console.log(email.slice(0, pos)); // strstr($email, '@', true)
console.log(email.indexOf("#") === -1);
输出
@example.com name true
PHP v8.6.0
v8.6.0
$email = 'name@example.com';
echo strstr($email, '@'), "\n"; // 从匹配处到末尾
echo strstr($email, '@', true), "\n"; // before_needle:匹配之前的部分
var_dump(strstr($email, 'zzz')); // 未命中:false(strpos 会返回位置)
输出
@example.com name bool(false)
Python v3.15.2
v3.15.2
email = "name@example.com"
head, sep, tail = email.partition("@") # partition:最接近 strstr 双形态的惯用法
print(sep + tail) # strstr($email, '@'):从匹配处到末尾
print(head) # strstr($email, '@', true):匹配之前的部分
print(email.find("#")) # 未命中:-1,如同 strpos 返回 false
输出
@example.com name -1
Rust v1.99.1
v1.99.1
fn main() {
let email = "name@example.com";
match email.find('@') {
// Option 强制处理未命中分支——即 PHP 的 false
Some(i) => {
println!("{}", &email[i..]); // strstr($email, '@')
println!("{}", &email[..i]); // strstr($email, '@', true)
}
None => println!("miss"),
}
println!("{}", email.find('#').is_none());
}
输出
@example.com name true
TypeScript v26.13.1
v26.13.1
const email: string = "name@example.com";
const pos: number = email.indexOf("@"); // -1 表未命中,如同 PHP 的 false
console.log(email.slice(pos)); // strstr($email, '@')
console.log(email.slice(0, pos)); // strstr($email, '@', true)
console.log(email.indexOf("#") === -1);
输出
@example.com name true
语言差异说明
PHP:strstr($h, $n) 命中返「从匹配处到末尾」的子串,第三个参数 before_needle=true 返匹配前部分;未命中返 false——只判断是否存在请用 str_contains()(手册 Note)。
PHP 双形态设计:strstr 返子串、strpos 返位置(独立条目已有完整对照);PHP 8.0 起空 needle 合法(返全文或空串),未命中判定必须用 === false。
C:strstr() 是本函数的同名原祖:命中返指针、未命中返 NULL,指针差即字节偏移(示例打印了偏移——那正是 strpos 的返回值)。
C++:find() 返位置(npos 表未命中),子串由 substr() 切——「位置/子串」两步走。
Go:strings.Index 返 -1/字节偏移,切片前必须判 -1,否则 panic。
Java/JS/TS:indexOf 返 -1,substring/slice 取子串。
Python:str.partition() 一次返回(前、分隔、后),是最贴近 strstr 双形态的惯用法;find 未命中返 -1。
Rust:find 返 Option<usize> 字节偏移,模式匹配 Some/None 强制处理未命中。
C#:IndexOf + Substring 组合,语义同上。
性能注(手册用户注):strstr 命中即复制子串,仅判存在时比 strpos 更耗内存。