ES6 - 新的字串方法 includes()



該方法確定字串是否為給定字串的子字串。

語法

str.includes(searchString[, position])

引數

  • searchString − 要搜尋的子字串。

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

返回值

如果字串包含子字串,則返回 true;否則返回 false

示例

var str = 'Hello World';  

console.log(str.includes('hell'))     
console.log(str.includes('Hell'));  

console.log(str.includes('or'));   
console.log(str.includes('or',1))

輸出

false 
true 
true 
true 
廣告
© . All rights reserved.