endsWith
endsWith() 方法用来判断当前字符串是否是以指定的子字符串结尾的。
如果传入的子字符串在搜索字符串的末尾则返回 true,否则将返回 false。
endsWith() 方法对大小写敏感。
let str = "Hello world, this is string test";
let res1 = str.endsWith("test")
let res2 = str.endsWith("Test")
let res3 = str.endsWith("rest")
console.log(res1,res2,res3)
运行结果:
语法
string.endsWith(searchvalue, length)
参数值
参数 | 描述 |
---|---|
searchvalue | 必需,要查找的子字符串。 |
length | 设置字符串的长度。默认值为原始字符串长度 string.length。 |
返回值
类型 | 描述 |
---|---|
Boolean | 如果字符串以指定的值结尾返回 true,否则返回 false。 |
例子
检查字符串是否以“string”结尾,假设字符串长度为27个字符:
let str = "Hello world, this is string test";
let res1 = str.endsWith("string",27)
let res2 = str.endsWith("String",27)
let res3 = str.endsWith("string",8)
console.log(res1,res2,res3)
运行结果: