// 示例代码:使用 endsWith() 方法检查字符串是否以指定的后缀结尾
// 定义一个字符串
let str = "Hello, world!";
// 检查字符串是否以 "world!" 结尾
let result1 = str.endsWith("world!");
console.log(result1); // 输出: true
// 检查字符串是否以 "JavaScript" 结尾
let result2 = str.endsWith("JavaScript");
console.log(result2); // 输出: false
// 使用 endsWith() 并指定位置参数,检查从指定位置开始的子字符串是否以指定后缀结尾
let result3 = str.endsWith("world", 11); // 11 是 "world" 的结束位置(不包括感叹号)
console.log(result3); // 输出: true
// 解释说明:
// endsWith() 方法用于判断调用它的字符串是否以指定的后缀结尾。
// 如果是,则返回 true;否则返回 false。
// 该方法还可以接受第二个参数,表示检查的起始位置(从左到右数)。
// 如果指定了位置参数,则只检查从字符串开头到该位置之间的子字符串。
上一篇:js charat()函数
下一篇:js 刷新iframe
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站