JavaScript String endsWith() 方法
实例
例子 1
检查字符串是否以 "world" 结尾:
let text = "Hello world";
let result = text.endsWith("world");
let text = "Hello World";
let result = text.endsWith("world");
例子 2
检查字符串的前 11 个字符是否以 "world" 结尾:
let text = "Hello world, welcome to the universe.";
text.endsWith("world", 11);
语法
string.endsWith(searchvalue, length)
参数
| 参数 | 描述 |
|---|---|
| searchvalue | 必需。要搜索的字符串。 |
| length |
可选。要搜索的字符串的长度。 默认值是字符串的长度。 |
返回值
| 类型 | 描述 |
|---|---|
| 布尔值 | 如果字符串以该值结尾,则为 true,否则为 false。 |
浏览器支持
endsWith() 是 ECMAScript6 (ES6) 特性。
所有浏览器都支持 ES6 (JavaScript 2015):
| Chrome | Edge | Firefox | Safari | Opera |
|---|---|---|---|---|
| Chrome | Edge | Firefox | Safari | Opera |
| 支持 | 支持 | 支持 | 支持 | 支持 |
Internet Explorer 11(及更早版本)不支持 endsWith()。