天天看点

JavaScript startsWith() 方法

JavaScript startsWith() 方法

JavaScript String 对象

查看字符串是否为 "Hello" 开头:

var str = "Hello world, welcome to the Runoob.";

var n = str.startsWith("Hello");

n 输出结果:

true

startsWith() 方法用于检测字符串是否以指定的子字符串开始。

如果是以指定的子字符串开头返回 true,否则 false。

startsWith() 方法对大小写敏感。

表格中的数字表示支持该属性的第一个浏览器版本号。

方法

startsWith()

41

12.0

17

9

28

参数

描述

searchvalue

必需,要查找的字符串。

start

可选,查找的开始位置,默认为 0。

类型

Boolean

如果字符串是以指定的子字符串开头返回 true,否则 false。

JavaScript 版本:

ECMAScript 6

查看从第 6 个索引位置是否以 "world" 开头:

var n = str.startsWith("world", 6);

JavaScript startsWith() 方法