JavaScript String endsWith() 方法

 JavaScript String 对象

endsWith()方法判断一个字符串是否以指定字符串的字符结束。

如果字符串以字符结尾,则endsWith()方法返回true,否则返回false

注意:此方法区分大小写。

语法:

string.endsWith(searchValue, length)
var str = 'www.nhooo.com';
str.endsWith('com');// true
测试看看‹/›

浏览器兼容性

表格中的数字指定了完全支持endsWith()方法的第一个浏览器版本:

Method
endsWith()411728912

参数值

参数描述
searchValue(必需)在此字符串末尾要搜索的字符
length(可选)指定要搜索的字符串的长度。如果省略,则默认值为字符串的长度

技术细节

返回值:如果在字符串的末尾找到给定的字符,则为true;否则为false
JavaScript版本:ECMAScript 6

更多示例

指定要搜索的字符串的长度:

var str = 'To be, or not to be, that is the question.';
str.endsWith('to be', 19);   // true
测试看看‹/›

 JavaScript String 对象