JavaScript String includes() 方法
- 上一頁 fromCharCode()
- 下一頁 indexOf()
- 返回上一層 JavaScript String 參考手冊
定義和用法
如果字符串包含指定的字符串,includes()
方法將返回 true
。
否則返回 false
。
includes()
方法區分大小寫。
實例
例子 1
檢查字符串是否包含 "world":
let text = "Hello world, welcome to the universe."; let result = text.includes("world");
let text = "Hello World, welcome to the universe."; let result = text.includes("world", 12);
例子 2
從位置 12 開始:
let text = "Hello world, welcome to the universe."; let result = text.includes("world", 12);
語法
string.includes(searchvalue, start)
參數
參數 | 描述 |
---|---|
searchvalue | 必需。要搜索的字符串。 |
start | 可選。開始的位置。默認值為 0。 |
返回值
類型 | 描述 |
---|---|
布爾值 | 如果字符串包含該值,則為 true ,否則為 false 。 |
瀏覽器支持
includes()
是 ECMAScript6 (ES6) 特性。
所有現代瀏覽器都支持 ES6 (JavaScript 2015):
Chrome | Edge | Firefox | Safari | Opera |
---|---|---|---|---|
Chrome | Edge | Firefox | Safari | Opera |
支持 | 支持 | 支持 | 支持 | 支持 |
Internet Explorer 11(或更早版本)不支持 includes()
。
- 上一頁 fromCharCode()
- 下一頁 indexOf()
- 返回上一層 JavaScript String 參考手冊