判断字符串是否以指定后缀(子字符串)结尾endswith()

 

let str = "Hello world";
str.endsWith("world")   // 返回 true
str.endsWith("World")   // 返回 false

endsWith() 方法用来判断当前字符串是否是以指定的子字符串结尾的(区分大小写)。

如果传入的子字符串在搜索字符串的末尾则返回 true,否则将返回 false。

语法

string.endsWith(searchvalue, length)

参数值

参数 描述
searchvalue 必需,要搜索的子字符串。
length 设置字符串的长度。默认值为原始字符串长度 string.length。

返回值

类型 描述
布尔值 如果字符串以指定的值结尾返回 true,否则返回 false。

技术细节

返回值: 布尔值,如果传入的子字符串在搜索字符串的末尾则返回true,否则将返回 false。
JavaScript 版本: ECMAScript 6

 

更多实例

设置不同字符串长度来判断:

var str = "To be, or not to be, that is the question.";
 
str.endsWith("question.");  // true
str.endsWith("to be");      // false
str.endsWith("to be", 19);  // true

 

posted @ 2023-02-23 11:21  SimoonJia  阅读(218)  评论(0编辑  收藏  举报