ES6+ endsWith()
我们学到了字符串的 startsWith()
用于判断是否以指定字符串为起始。 本节介绍 ES6 中新增的与之相反的字符串 endsWith()
,该用来判断当前字符串是否是以另外给定的子字符串为结尾。
endsWith()
用于判断当前字符串,是否以指定字符串为结尾的,如果在字符串的结尾找到了给定的字符则返回 true,否则返回 false。
使用语法:
str.endsWith(searchString[, length])
参数说明:
实例:
const str1 = 'Cats are the best!';
console.log(str1.endsWith('best', )); // true
const str2 = 'Is this a question';
console.log(str2.endsWith('?')); // false
字符串是否在另字符串的末尾。
这里需要说明一下的是,当字符串 endsWith()
时不传参数时,认是 undefined
返回的结果是 false。
var str = "I love imooc.";
console.log(str.endsWith()); // false
console.log(str.endsWith(undefined)); // false
上面的中,第 2 行和第 3 行是等价的,因为第参数是必填的,所以在当我们没有传参时,认使用 undefined
来填充,注意这里不是字符串类型的 ‘undefined’
var str = "I love imooc.";
console.log(str.endsWith("I love")); // false
console.log(str.endsWith("imooc")); // false
console.log(str.endsWith("imooc.")); // true
console.log(str.endsWith("")); // true
从例子中我们可以看出,只有结尾有最后字符的时候,才会返回 true,只要没有包含结尾的字符,即使查找的字符串在目标字符串里也是返回 fasle 的。在查找空字符串时,返回的结果是 true,那是因为空字符在任何字符串中都是存在的。
当有第二个参数的时候,第二个参数是字符串的长度
var str = "I love imooc.";
console.log(str.endsWith("love", )); // true
console.log(str.endsWith("e", )); // true
从这两个 log 打印出来的结果可以看出,第二个参数会取原字符串的指定长度作为查找的目标字符串,这里的第二个参数是 6 也就是取原字符串的 I love
,所以 endsWith
判断是以 love
结尾的。
在字符串中的结尾时最好使用 endsWith
进行,它的效率要比 includes()
高,而且 endsWith
也具有语义化。