JavaScript参考手册
字符串 | String
string.endsWith
endsWith()
方法用来判断当前字符串是否是以另外一个给定的子字符串“结尾”的,根据判断结果返回 true
或 false
。
语法
str.endsWith(searchString[, length])
参数
searchString
要搜索的子字符串。position
在 str 中搜索 searchString 的结束位置,默认值为str.length
,也就是真正的字符串结尾处。
返回值
如果给定的字符在string尾部找到,返回true
否则返回false
.
描述
此方法可让您确定一个字符串是否以另一个字符串结尾。这种方法是区分大小写的。
示例
使用endsWith()
var str = 'To be, or not to be, that is the question.';
console.log(str.endsWith('question.')); // true
console.log(str.endsWith('to be')); // false
console.log(str.endsWith('to be', 19)); // true
备注
此方法已被添加到ECMAScript 6规范中,可能尚未在所有JavaScript实现中提供。但是,您可以String.prototype.endsWith()
使用以下代码片段进行填充:
if (!String.prototype.endsWith)
String.prototype.endsWith = function(searchStr, Position) {
// This works much better than >= because
// it compensates for NaN:
if (!(Position < this.length))
Position = this.length;
else
Position |= 0; // round position
return this.substr(Position - searchStr.length,
searchStr.length) === searchStr;
};
规范
Specification |
Status |
Comment |
---|---|---|
ECMAScript 2015 (6th Edition, ECMA-262)The definition of 'String.prototype.endsWith' in that specification. |
Standard |
Initial definition. |
ECMAScript Latest Draft (ECMA-262)The definition of 'String.prototype.endsWith' in that specification. |
Living Standard |
|
浏览器兼容性
Feature |
Chrome |
Edge |
Firefox |
Internet Explorer |
Opera |
Safari |
---|---|---|---|---|---|---|
Basic Support |
41 |
(Yes) |
17 |
No |
28 |
9 |
Feature |
Android |
Chrome for Android |
Edge mobile |
Firefox for Android |
IE mobile |
Opera Android |
iOS Safari |
---|---|---|---|---|---|---|---|
Basic Support |
(Yes) |
36 |
(Yes) |
17 |
No |
(Yes) |
9 |
字符串 | String相关
JavaScript 是一种高级编程语言,通过解释执行,是一门动态类型,面向对象(基于原型)的解释型语言。它已经由ECMA(欧洲电脑制造商协会)通过 ECMAScript 实现语言的标准化。它被世界上的绝大多数网站所使用,也被世界主流浏览器( Chrome、IE、FireFox、Safari、Opera )支持。JavaScript 是一门基于原型、函数先行的语言,是一门多范式的语言,它支持面向对象编程,命令式编程,以及函数式编程。它提供语法来操控文本、数组、日期以及正则表达式等,不支持 I/O,比如网络