JavaScript String endsWith() 方法
示例
檢查一個字串是否以 "world" 結尾
let text = "Hello world";
let result = text.endsWith("world");
自己動手試一試 »
let text = "Hello World";
let result = text.endsWith("world");
自己動手試一試 »
更多示例見下文。
描述
endsWith()
方法如果字串以指定字串結尾,則返回 true
。
否則返回 false
。
endsWith()
方法區分大小寫。
另請參閱
語法
string.endsWith(searchvalue, length)
引數
引數 | 描述 |
searchvalue | 必需。 要搜尋的字串。 |
length | 可選。 要搜尋的字串的長度。 預設值是字串的長度。 |
返回值
型別 | 描述 |
一個布林值 | 如果字串以該值結尾,則為 true ,否則為 false 。 |
更多示例
檢查字串的前 11 個字元是否以 "world" 結尾
let text = "Hello world, welcome to the universe.";
text.endsWith("world", 11);
自己動手試一試 »
瀏覽器支援
endsWith()
是 ECMAScript6 (ES6) 的一個特性。
自 2017 年 6 月起,所有現代瀏覽器都支援 ES6 (JavaScript 2015)
Chrome 51 | Edge 15 | Firefox 54 | Safari 10 | Opera 38 |
2016 年 5 月 | 2017 年 4 月 | 2017 年 6 月 | 2016 年 9 月 | 2016 年 6 月 |
endsWith()
在 Internet Explorer 中不受支援。