Метод endsWith()
визначає, чи завершується рядок символами вказаного рядка, повертаючи, відповідно, true
чи false
.
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
Синтаксис
str.endsWith(searchString[, length])
Параметри
searchString
- Символи, які шукатимуться в кінці рядка
str
. length
Optional- Якщо наданий, використовується як довжина
str
. За замовчуванням дорівнюєstr.length
.
Значення, що повертається
true
, якщо надані символи знайдені в кінці рядка; інакше, false
.
Опис
Цей метод дозволяє визначити, чи завершується рядок іншим рядком. Цей метод чутливий до регістру.
Приклади
Використання endsWith()
let str = 'Питання в тому: бути чи не бути.'
console.log(str.endsWith('бути.')) // true
console.log(str.endsWith('Питання')) // false
console.log(str.endsWith('Питання', 7)) // 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;
};
}
Специфікації
Специфікація |
---|
ECMAScript (ECMA-262) The definition of 'String.prototype.endsWith' in that specification. |
Сумісність з веб-переглядачами
BCD tables only load in the browser
The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.