str_ends_with
str_ends_with(string $haystack, string $needle): bool
判断字符串是否以指定后缀结尾。
C C23
C23
#include <stdio.h>
#include <string.h>
// 手写惯用法:指针推进到尾部,比较等长片段
static int str_ends_with(const char *h, const char *n) {
size_t lh = strlen(h), ln = strlen(n);
if (ln > lh) return 0;
return strcmp(h + lh - ln, n) == 0;
}
int main(void) {
// 打印三组判断:是后缀、不是后缀、空后缀恒真
printf("%d %d %d\n", str_ends_with("photo.jpg", ".jpg"),
str_ends_with("photo.jpg", ".png"), str_ends_with("photo", ""));
return 0;
}
输出
1 0 1
C++ C++26
C++26
#include <iostream>
#include <string>
// 手写惯用法:尾部等长片段做 compare
static bool str_ends_with(const std::string &h, const std::string &n) {
return h.size() >= n.size() &&
h.compare(h.size() - n.size(), n.size(), n) == 0;
}
int main() {
// 打印三组判断:是后缀、不是后缀、空后缀恒真
std::cout << str_ends_with("photo.jpg", ".jpg") << " "
<< str_ends_with("photo.jpg", ".png") << " "
<< str_ends_with("photo", "") << "\n";
return 0;
}
输出
1 0 1
C# v11.0.100
v11.0.100
// EndsWith 判断后缀;空串恒为 true
Console.WriteLine("photo.jpg".EndsWith(".jpg"));
Console.WriteLine("photo.jpg".EndsWith(".png"));
Console.WriteLine("photo".EndsWith(""));
输出
True False True
Go v1.27.4
v1.27.4
package main
import (
"fmt"
"strings"
)
func main() {
// strings.HasSuffix 判断后缀;空串恒为 true
fmt.Println(strings.HasSuffix("photo.jpg", ".jpg"))
fmt.Println(strings.HasSuffix("photo.jpg", ".png"))
fmt.Println(strings.HasSuffix("photo", ""))
}
输出
true false true
Java v27.0.1
v27.0.1
public class Main {
public static void main(String[] args) {
// endsWith 判断后缀;空串恒为 true
System.out.println("photo.jpg".endsWith(".jpg"));
System.out.println("photo.jpg".endsWith(".png"));
System.out.println("photo".endsWith(""));
}
}
输出
true false true
JavaScript v26.13.1
v26.13.1
// endsWith 判断后缀(ES2015 引入;空串恒为 true)
console.log("photo.jpg".endsWith(".jpg"));
console.log("photo.jpg".endsWith(".png"));
console.log("photo".endsWith(""));
输出
true false true
PHP v8.6.0
v8.6.0
// PHP 8.0 起可用:判断后缀,直接返回布尔值
var_dump(str_ends_with("photo.jpg", ".jpg"));
var_dump(str_ends_with("photo.jpg", ".png"));
// 空后缀恒为 true
var_dump(str_ends_with("photo", ""));
输出
bool(true) bool(false) bool(true)
Python v3.15.2
v3.15.2
# endswith 判断后缀(空串恒为 True)
print("photo.jpg".endswith(".jpg"))
print("photo.jpg".endswith(".png"))
print("photo".endswith(""))
输出
True False True
Rust v1.99.1
v1.99.1
fn main() {
// ends_with 判断后缀;空串恒为 true
println!("{}", "photo.jpg".ends_with(".jpg"));
println!("{}", "photo.jpg".ends_with(".png"));
println!("{}", "photo".ends_with(""));
}
输出
true false true
TypeScript v26.13.1
v26.13.1
// endsWith 判断后缀(ES2015 引入;空串恒为 true)
console.log("photo.jpg".endsWith(".jpg"));
console.log("photo.jpg".endsWith(".png"));
console.log("photo".endsWith(""));
输出
true false true
语言差异说明
C:惯用法是长度守卫后 strcmp 尾部片段;C23 标准库也没有 ends_with。
C++:惯用法 size() 守卫 + compare(尾部偏移, 长度, $needle);C++20 起 ends_with 才标准化。
C#:string.EndsWith。
Go:strings.HasSuffix。
Java:String.endsWith。
JavaScript:String.prototype.endsWith(ES2015 引入;此前惯用正则 $ 锚点或 slice 截尾比较)。
PHP:PHP 8.0 引入,取代老写法 substr($haystack, -strlen($needle)) === $needle(负偏移截尾,needle 为空时 substr 返回全串需注意)。
Python:str.endswith(还支持元组参数一次判断多个后缀,常见于文件扩展名过滤)。
Rust:str::ends_with。
TypeScript:同 JavaScript 的 endsWith。