ES6 - 新的字串方法 startsWith



此方法用於判斷字串是否以指定的字元開頭。

語法

str.startsWith(searchString[, position])

引數

  • searchString − 要在該字串開頭搜尋的字元。

  • Position − 在此字串中開始搜尋 searchString 的位置;預設為 0。

返回值

如果字串以搜尋字串的字元開頭,則返回 true;否則返回 false

示例

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

輸出

true 
廣告
© . All rights reserved.