定义和用法
startsWith() 方法用于检测字符串是否以指定的子字符串开始。
如果是以指定的子字符串开头返回 true,否则 false。
startsWith() 方法对大小写敏感。
1:查看字符串是否为 "Hello" 开头:
var str = "Hello world, welcome to the Runoob."; var n = str.startsWith("Hello");
n 输出结果:
true
2:查看从第 6 个索引位置是否以 "world" 开头:
var str = "Hello world, welcome to the Runoob."; var n = str.startsWith("world", 6);
n 输出结果:
true