// 示例代码:使用 startsWith 方法检查字符串是否以指定的子字符串开头
// 定义一个字符串
const str = "Hello, world!";
// 使用 startsWith 方法检查字符串是否以 "Hello" 开头
const result1 = str.startsWith("Hello"); // 返回 true
// 使用 startsWith 方法检查字符串是否以 "world" 开头
const result2 = str.startsWith("world"); // 返回 false
// 也可以指定开始位置,从索引 7 开始检查是否以 "world" 开头
const result3 = str.startsWith("world", 7); // 返回 true
console.log(result1); // 输出: true
console.log(result2); // 输出: false
console.log(result3); // 输出: true
// 解释说明:
// startsWith 方法用于检查字符串是否以指定的子字符串开头。
// 如果匹配成功,则返回 true;否则返回 false。
// 可以通过第二个参数指定开始搜索的位置,默认是从字符串的第一个字符开始。
上一篇:js iterator
下一篇:js alert()的功能
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站