// 示例代码:使用 startsWith() 函数
// 定义一个字符串
let str = "Hello, world!";
// 检查字符串是否以 "Hello" 开头
let result1 = str.startsWith("Hello");
console.log(result1); // 输出: true
// 检查字符串是否以 "world" 开头,从索引 7 开始检查
let result2 = str.startsWith("world", 7);
console.log(result2); // 输出: true
// 检查字符串是否以 "Hi" 开头
let result3 = str.startsWith("Hi");
console.log(result3); // 输出: false
// 解释说明:
// startsWith() 方法用于判断当前字符串是否以指定的子字符串开头。
// 它返回一个布尔值 (true 或 false)。
// 如果提供了第二个参数,则从该索引位置开始检查子字符串是否匹配。
上一篇:js parseint
下一篇:js unshif()函数
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站