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