strcmp / strncmp
strcmp(string $string1, string $string2): int — strncmp(string $string1, string $string2, int $length): int
二进制安全字符串比较:返回差值符号(<0/0/>0)而非布尔——只有符号可移植(PHP 8.2 起不再保证返回 strlen 差);strncmp 只比前 n 个字符。
C C23
C23
#include <stdio.h>
#include <string.h>
int main(void) {
// strcmp 返回差值符号;只有符号是可移植语义
printf("%d\n", strcmp("apple", "apple"));
printf("%s\n", strcmp("apple", "apricot") < 0 ? "lt" : "ge");
printf("%s\n", strncmp("photo.jpg", "photo.png", 6) == 0 ? "same prefix" : "different");
return 0;
}
输出
0 lt same prefix
C++ C++26
C++26
#include <iostream>
#include <string>
int main() {
// std::string::compare 与 strcmp 同符号约定
std::string a = "apple";
std::string p = "photo.jpg";
std::string q = "photo.png";
std::cout << a.compare("apple") << "\n";
std::cout << (a.compare("apricot") < 0 ? "lt" : "ge") << "\n";
std::cout << (p.compare(0, 6, q, 0, 6) == 0 ? "same prefix" : "different") << "\n";
}
输出
0 lt same prefix
C# v11.0.100
v11.0.100
// string.CompareOrdinal 给出符号;无 strncmp——用带下标的长度限定比较等价
Console.WriteLine(string.CompareOrdinal("apple", "apple"));
Console.WriteLine(string.CompareOrdinal("apple", "apricot") < 0 ? "lt" : "ge");
Console.WriteLine(string.CompareOrdinal("photo.jpg", 0, "photo.png", 0, 6) == 0 ? "same prefix" : "different");
输出
0 lt same prefix
Go v1.27.4
v1.27.4
package main
import (
"fmt"
"strings"
)
func main() {
// strings.Compare 归一化为 -1/0/+1
fmt.Println(strings.Compare("apple", "apple"))
if strings.Compare("apple", "apricot") < 0 {
fmt.Println("lt")
} else {
fmt.Println("ge")
}
if "photo.jpg"[:6] == "photo.png"[:6] { // 无 strncmp——切片后再比
fmt.Println("same prefix")
} else {
fmt.Println("different")
}
}
输出
0 lt same prefix
Java v27.0.1
v27.0.1
public class Main {
public static void main(String[] args) {
// String.compareTo 即 strcmp;regionMatches 即 strncmp
System.out.println("apple".compareTo("apple"));
System.out.println("apple".compareTo("apricot") < 0 ? "lt" : "ge");
System.out.println("photo.jpg".regionMatches(0, "photo.png", 0, 6) ? "same prefix" : "different");
}
}
输出
0 lt same prefix
JavaScript v26.13.1
v26.13.1
// JS 无 strcmp:=== 判等,< / > 给字典序符号
const a = "apple";
console.log(a === "apple" ? 0 : a < "apple" ? -1 : 1);
console.log(a < "apricot" ? "lt" : "ge");
console.log("photo.jpg".slice(0, 6) === "photo.png".slice(0, 6) ? "same prefix" : "different");
输出
0 lt same prefix
PHP v8.6.0
v8.6.0
// strcmp 返回差值符号——绝不要断言具体数值;
// 只有符号可移植(PHP 8.2 起可能返回 -1/1 而非 strlen 差值)
echo strcmp("apple", "apple"), "\n";
echo strcmp("apple", "apricot") < 0 ? "lt" : "ge", "\n";
echo strncmp("photo.jpg", "photo.png", 6) == 0 ? "same prefix" : "different", "\n";
输出
0 lt same prefix
Python v3.15.2
v3.15.2
# Python:str 的 == 与 < 即无符号版等价物;无 strncmp——切片后再比
print(0 if "apple" == "apple" else 1)
print("lt" if "apple" < "apricot" else "ge")
print("same prefix" if "photo.jpg"[:6] == "photo.png"[:6] else "different")
输出
0 lt same prefix
Rust v1.99.1
v1.99.1
use std::cmp::Ordering;
fn main() {
// Ord::cmp 返回类型化的 Ordering——显式化的符号
let ord = "apple".cmp(&"apple");
println!("{}", match ord {
Ordering::Equal => 0,
Ordering::Less => -1,
Ordering::Greater => 1,
});
println!("{}", if "apple" < "apricot" { "lt" } else { "ge" });
println!("{}", if &"photo.jpg"[..6] == &"photo.png"[..6] { "same prefix" } else { "different" });
}
输出
0 lt same prefix
TypeScript v26.13.1
v26.13.1
// TS 无 strcmp:=== 判等,< / > 给字典序符号
const a: string = "apple";
console.log(a === "apple" ? 0 : a < "apple" ? -1 : 1);
console.log(a < "apricot" ? "lt" : "ge");
console.log("photo.jpg".slice(0, 6) === "photo.png".slice(0, 6) ? "same prefix" : "different");
输出
0 lt same prefix
语言差异说明
C:strcmp/strncmp 是原版;返回值只有符号可移植,具体差值是实现细节。
C++:string::compare 同符号约定。
C#:CompareOrdinal 是序数比较(无本地化);带下标重载即 strncmp。
Go:strings.Compare 归一化 -1/0/+1;切片比较是惯用 strncmp。
Java:compareTo 即 strcmp;regionMatches 即 strncmp。
JavaScript:无独立函数,===/< 组合即等价物。
PHP:== 是宽松比较(PHP 8 起数字字符串按数值比),比较字符串内容请用 strcmp;绝不用 strcmp 比密码/哈希——早退特性有计时侧信道,比较哈希请用 hash_equals(hash_equals 条目);strncmp 的长度参数防越界。
Python:</== 直接可用;多重比较无 strncmp 需求时切片即可。
Rust:Ord::cmp 返回类型化 Ordering,编译器强制处理三态。
TypeScript:同 JavaScript。