String.prototype.endsWith()

endsWith() 用来判断当前字符串是否是以另外一个给定的子字符串 结尾 的,根据判断结果返回 truefalse

语法

语法:

str.endsWith( searchString [, endPosition] )

类型声明:

interface String {
  endsWith(seachString: string, endPosition?: number): boolean;
}

参数说明:

参数 说明 类型
searchString 要搜索的子字符串 string
endPosition 作为 str 的长度 number

这个方法帮助你确定一个字符串是否在另一个字符串的末尾。这个方法是大小写敏感的。

方法说明

concat 方法将一个或多个字符串与原字符串连接合并,形成一个新的字符串并返回。 concat 方法并不影响原字符串。

代码示例

const str = 'Hello world!';

console.log(str.endsWith('world!'));
// true

console.log(str.endsWith('abc'));
// false

兼容性代码

if (!String.prototype.endsWith) {
  String.prototype.endsWith = function (searchString, endPosition) {
    if (endPosition === undefined || endPosition > this.length) {
      endPosition = this.length;
    }
    return this.substring(endPosition - searchString.length, endPosition) === searchString;
  };
}

参考资料