JavaScript String endsWith() 方法
示例
检查一个字符串是否以 "world" 结尾
let text = "Hello world";
let result = text.endsWith("world");
自己动手试一试 »
let text = "Hello World";
let result = text.endsWith("world");
自己动手试一试 »
更多示例见下文。
描述
endsWith()
方法如果字符串以指定字符串结尾,则返回 true
。
否则返回 false
。
endsWith()
方法区分大小写。
另请参阅
语法
string.endsWith(searchvalue, length)
参数
参数 | 描述 |
searchvalue | 必需。 要搜索的字符串。 |
length | 可选。 要搜索的字符串的长度。 默认值是字符串的长度。 |
返回值
类型 | 描述 |
一个布尔值 | 如果字符串以该值结尾,则为 true ,否则为 false 。 |
更多示例
检查字符串的前 11 个字符是否以 "world" 结尾
let text = "Hello world, welcome to the universe.";
text.endsWith("world", 11);
自己动手试一试 »
浏览器支持
endsWith()
是 ECMAScript6 (ES6) 的一个特性。
自 2017 年 6 月起,所有现代浏览器都支持 ES6 (JavaScript 2015)
Chrome 51 | Edge 15 | Firefox 54 | Safari 10 | Opera 38 |
2016 年 5 月 | 2017 年 4 月 | 2017 年 6 月 | 2016 年 9 月 | 2016 年 6 月 |
endsWith()
在 Internet Explorer 中不受支持。