ES6 - 新字符串方法 startsWith

该方法确定字符串是否以指定字符开头。

语法

str.startsWith(searchString[, position])

参数

  • searchString − 在此字符串开头要搜索的字符。

  • Position − 在此字符串中开始搜索 searchString 的位置;默认为 0。

返回值

如果字符串以搜索字符串的字符开头,则为 true;否则为 false

示例

var str = 'hello world!!!'; 
console.log(str.startsWith('hello'));

输出

true