สตริง JavaScript ประกอบด้วย ()
ตัวอย่าง
ตรวจสอบว่าสตริงมี "โลก" หรือไม่:
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);
ตัวอย่างเพิ่มเติมด้านล่าง
ความหมายและการใช้งาน
includes()
เมธอดส่งคืนหากtrue
สตริงมีสตริงที่ระบุ
มิฉะนั้นจะกลับfalse
มา
includes()
วิธีการนี้คำนึงถึงขนาดตัวพิมพ์
ไวยากรณ์
string.includes(searchvalue, start)
พารามิเตอร์
Parameter | Description |
searchvalue | Required. The string to search for. |
start | Optional. The position to start from. Default value is 0. |
คืนมูลค่า
พิมพ์ | คำอธิบาย |
บูลีน | true ถ้าสตริงมีค่า มิfalse ฉะนั้น |
ตัวอย่างเพิ่มเติม
เริ่มต้นที่ตำแหน่ง 12:
let text = "Hello world, welcome to the universe.";
let result = text.includes("world", 12);
รองรับเบราว์เซอร์
includes()
เป็นคุณลักษณะ ECMAScript6 (ES6)
ES6 (JavaScript 2015) ได้รับการสนับสนุนในเบราว์เซอร์ที่ทันสมัยทั้งหมด:
Chrome | Edge | Firefox | Safari | Opera |
Yes | Yes | Yes | Yes | Yes |
includes()
ไม่รองรับใน Internet Explorer 11 (หรือเก่ากว่า)