--- title: String.prototype.endsWith() slug: Web/JavaScript/Reference/Global_Objects/String/endsWith tags: - JavaScript - Method - Prototype - Reference - String translation_of: Web/JavaScript/Reference/Global_Objects/String/endsWith ---
endsWith() メソッドは文字列が引数で指定された文字列で終わるかを判定して true か false を返します。
str.endsWith(searchString[, length])
searchStringstr の末尾で検索される文字の集合です。length {{optional_inline}}str の長さとして使用されます。既定値は str.length です。文字列が指定された文字列で終わる場合は true、それ以外の場合は false です。
文字列が特定の文字列で終わるかどうかを判断できます。このメソッドでは (英文字の) 大文字・小文字は区別されます。
let str = 'To be, or not to be, that is the question.'
console.log(str.endsWith('question.')) // true
console.log(str.endsWith('to be')) // false
console.log(str.endsWith('to be', 19)) // true
このメソッドは ECMAScript 6 で追加されました。すべての JavaScript の実装でまだ利用可能ではないかもしれません。しかしながら、次のコードで String.prototype.endsWith() をエミュレートできます。
if (!String.prototype.endsWith) {
String.prototype.endsWith = function(search, this_len) {
if (this_len === undefined || this_len > this.length) {
this_len = this.length;
}
return this.substring(this_len - search.length, this_len) === search;
};
}
| 仕様書 |
|---|
| {{SpecName('ESDraft', '#sec-string.prototype.endswith', 'String.prototype.endsWith')}} |
{{Compat("javascript.builtins.String.endsWith")}}