--- title: String.prototype.endsWith() slug: Web/JavaScript/Reference/Global_Objects/String/endsWith tags: - JavaScript - Méthode - Prototyp - Referenz - String translation_of: Web/JavaScript/Reference/Global_Objects/String/endsWith ---
Die Methode endsWith()
bestimmt, ob ein String das Ende eines anderen Strings ist, und liefert entsprechend true oder false zurück.
str.endsWith(suchString[, position])
suchString
position
Falls suchString das Ende von str ist, wird
true
zurückgeliefert, andernfalls wird false
zurückgeliefert.
Diese Methode bestimmt, ob ein String das Ende eines anderen Strings ist. Die Methode unterscheidet zwischen Groß- und Kleinschreibung.
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
Diese Methode wurde der ECMAScript6-Spezifikation hinzugefügt und könnte noch nicht in allen JavaScript-Implementierungen verfügbar sein. Mithilfe des folgenden Code-Stücks kann die Methode auch bei fehlender Implementierung genutzt werden:
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; }; }
Specifikation | Status | Kommentar |
---|---|---|
{{SpecName('ES6', '#sec-string.prototype.endswith', 'String.prototype.endsWith')}} | {{Spec2('ES6')}} | Initiale Definition. |
{{SpecName('ESDraft', '#sec-string.prototype.endswith', 'String.prototype.endsWith')}} | {{Spec2('ESDraft')}} |
Feature | Chrome | Firefox (Gecko) | Edge | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|---|
Grundlegende Unterstützung | {{CompatChrome("41")}} | {{CompatGeckoDesktop("17")}} | {{CompatVersionUnknown}} | {{CompatNo}} | {{CompatNo}} | {{CompatSafari("9")}} |
Feature | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|
Grundlegende Unterstützung | {{CompatNo}} | {{CompatChrome("36")}} | {{CompatGeckoMobile("17")}} | {{CompatNo}} | {{CompatNo}} | {{CompatNo}} |