--- title: String.prototype.endsWith() slug: Web/JavaScript/Reference/Global_Objects/String/endsWith tags: - JavaScript - Prototipo - Referencia - String - endsWith() - metodo translation_of: Web/JavaScript/Reference/Global_Objects/String/endsWith ---
O método endsWith()
indica se uma string termina com determinados caracteres, retornando true
ou false
.
str.endsWith(stringSendoBuscada[, tamanho])
stringSendoBuscada
tamanho
true
se os caracteres passados forem encontrados no final da string. Do contrário, retorna false
.
Este método permite que você verifique se uma string termina ou não com determinados caracteres. Este método é case-sensitive.
endsWith()
var str = 'Ser ou não ser, eis a questão'; console.log(str.endsWith('questão')); // retorna true console.log(str.endsWith('ser')); // retorna false console.log(str.endsWith('ser', 14)); // retorna true
Este método foi adicionada na especificação ECMAScript 6 e talvez não esteja disponível em todos as implementações JavaScript ainda. No entanto, você pode criá-lo adicionando o seguinte código:
if (!String.prototype.endsWith) String.prototype.endsWith = function(searchStr, Position) { // This works much better than >= because // it compensates for NaN: if (!(Position < this.length)) Position = this.length; else Position |= 0; // round position return this.substr(Position - searchStr.length, searchStr.length) === searchStr; };
Especificação | Status | Comentário |
---|---|---|
{{SpecName('ES6', '#sec-string.prototype.endswith', 'String.prototype.endsWith')}} | {{Spec2('ES6')}} | Definição inicial. |
{{SpecName('ESDraft', '#sec-string.prototype.endswith', 'String.prototype.endsWith')}} | {{Spec2('ESDraft')}} |
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.
{{Compat("javascript.builtins.String.endsWith")}}