str_starts_with
str_starts_with(string $haystack, string $needle): bool
判断字符串是否以指定前缀开头。
C C23
C23
#include <stdio.h>
#include <string.h>
// 手写惯用法:strncmp 只比较前缀长度,相等返回 0
static int str_starts_with(const char *h, const char *n) {
return strncmp(h, n, strlen(n)) == 0;
}
int main(void) {
// 打印三组判断:是前缀、不是前缀、空前缀恒真
printf("%d %d %d\n", str_starts_with("Hello world", "Hello"),
str_starts_with("Hello world", "world"), str_starts_with("Hello", ""));
return 0;
}
输出
1 0 1
C++ C++26
C++26
#include <iostream>
#include <string>
// 手写惯用法:rfind 限定从位置 0 起找,命中即前缀
static bool str_starts_with(const std::string &h, const std::string &n) {
return h.rfind(n, 0) == 0;
}
int main() {
// 打印三组判断:是前缀、不是前缀、空前缀恒真
std::cout << str_starts_with("Hello world", "Hello") << " "
<< str_starts_with("Hello world", "world") << " "
<< str_starts_with("Hello", "") << "\n";
return 0;
}
输出
1 0 1
C# v11.0.100
v11.0.100
// StartsWith 判断前缀;空串恒为 true
Console.WriteLine("Hello world".StartsWith("Hello"));
Console.WriteLine("Hello world".StartsWith("world"));
Console.WriteLine("Hello".StartsWith(""));
输出
True False True
Go v1.27.4
v1.27.4
package main
import (
"fmt"
"strings"
)
func main() {
// strings.HasPrefix 判断前缀;空串恒为 true
fmt.Println(strings.HasPrefix("Hello world", "Hello"))
fmt.Println(strings.HasPrefix("Hello world", "world"))
fmt.Println(strings.HasPrefix("Hello", ""))
}
输出
true false true
Java v27.0.1
v27.0.1
public class Main {
public static void main(String[] args) {
// startsWith 判断前缀;空串恒为 true
System.out.println("Hello world".startsWith("Hello"));
System.out.println("Hello world".startsWith("world"));
System.out.println("Hello".startsWith(""));
}
}
输出
true false true
JavaScript v26.13.1
v26.13.1
// startsWith 判断前缀(ES2015 引入;空串恒为 true)
console.log("Hello world".startsWith("Hello"));
console.log("Hello world".startsWith("world"));
console.log("Hello".startsWith(""));
输出
true false true
PHP v8.6.0
v8.6.0
// PHP 8.0 起可用:判断前缀,直接返回布尔值
var_dump(str_starts_with("Hello world", "Hello"));
var_dump(str_starts_with("Hello world", "world"));
// 空前缀恒为 true
var_dump(str_starts_with("Hello", ""));
输出
bool(true) bool(false) bool(true)
Python v3.15.2
v3.15.2
# startswith 判断前缀(空串恒为 True)
print("Hello world".startswith("Hello"))
print("Hello world".startswith("world"))
print("Hello".startswith(""))
输出
True False True
Rust v1.99.1
v1.99.1
fn main() {
// starts_with 判断前缀;空串恒为 true
println!("{}", "Hello world".starts_with("Hello"));
println!("{}", "Hello world".starts_with("world"));
println!("{}", "Hello".starts_with(""));
}
输出
true false true
TypeScript v26.13.1
v26.13.1
// startsWith 判断前缀(ES2015 引入;空串恒为 true)
console.log("Hello world".startsWith("Hello"));
console.log("Hello world".startsWith("world"));
console.log("Hello".startsWith(""));
输出
true false true
语言差异说明
C:惯用法 strncmp($haystack, $needle, strlen($needle)) == 0;空 needle 时 strlen 为 0,strncmp 返回 0 故恒真。
C++:惯用法 rfind($needle, 0) == 0;C++20 起 starts_with 才标准化。
C#:string.StartsWith。
Go:strings.HasPrefix。
Java:String.startsWith。
JavaScript:String.prototype.startsWith(ES2015 引入;此前惯用 lastIndexOf($needle, 0) === 0 或正则)。
PHP:PHP 8.0 引入,取代老写法 substr($haystack, 0, strlen($needle)) === $needle(需先算长度再截取再全等,繁琐且易错)。
Python:str.startswith(还支持元组参数一次判断多个前缀)。
Rust:str::starts_with。
TypeScript:同 JavaScript 的 startsWith。