Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

String.prototype.endsWith()

Tento překlad není kompletní. Prosím pomozte přeložit tento článek z angličtiny.

Metoda endsWith() zjišťuje, jestli řetězec končí znaky jiného řetězce. Výsledkem je hodnota true nebo false.

Syntaxe

str.endsWith(hledanyRetezec[, pozice])

Parametry

hledanyRetezec
Znaky hledané na konci tohoto řetězce.
pozice
Volitelné. Omezení prohledávané délky řetězce. Výchozí hodnota je skutečná délka řetězce.

Popis

Tato metoda umožní zjistit, zda řetězec končí znaky jiného řetězce. Prohledávání je case-sensitive.

Příklady

Použití endsWith()

var 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

Polyfill

Tato metoda byla přidána do specifikace ECMAScript 6 a nemusí být ještě dostupná ve všech implementacích jazyka JavaScript. Nicméně, můžete ji doplnit pomocí následujícího kódu:

if (!String.prototype.endsWith) {
  String.prototype.endsWith = function(searchString, position) {
      var subjectString = this.toString();
      if (typeof position !== 'number' || !isFinite(position) || Math.floor(position) !== position || position > subjectString.length) {
        position = subjectString.length;
      }
      position -= searchString.length;
      var lastIndex = subjectString.indexOf(searchString, position);
      return lastIndex !== -1 && lastIndex === position;
  };
}

Specifikace

Specifikace Status Komentář
ECMAScript 2015 (6th Edition, ECMA-262)
The definition of 'String.prototype.endsWith' in that specification.
Standard První definice.
ECMAScript 2017 Draft (ECMA-262)
The definition of 'String.prototype.endsWith' in that specification.
Draft  

Kompatibilita s prohlížeči

Funkce Chrome Firefox (Gecko) Edge Internet Explorer Opera Safari
Základní podpora 41 17 (17) (Yes) No support No support 9
Funkce Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Základní podpora No support 36 17.0 (17) No support No support No support

Viz také

Štítky a přispěvatelé do dokumentace

 Přispěvatelé této stránky: lukaskopenec
 Poslední aktualizace od: lukaskopenec,