// 示例代码:判断字符串是否以指定字符或子字符串开头
// 使用 startsWith 方法
let str = "Hello, world!";
let prefix = "Hello";
if (str.startsWith(prefix)) {
console.log("字符串以 '" + prefix + "' 开头");
} else {
console.log("字符串不以 '" + prefix + "' 开头");
}
// 解释说明:
// startsWith 方法用于检查一个字符串是否以指定的子字符串开头。
// 它返回一个布尔值,true 表示确实以该子字符串开头,false 表示不是。
// 你可以传递第二个参数来指定从哪个索引开始检查。例如:
// str.startsWith("world", 7) 会返回 true,因为它从索引 7 开始匹配 "world"。
上一篇:js 判断是否字符串
下一篇:js 判断字符串开头
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站