diff options
Diffstat (limited to 'files/de/web/javascript/reference/global_objects/string/indexof')
-rw-r--r-- | files/de/web/javascript/reference/global_objects/string/indexof/index.html | 200 |
1 files changed, 200 insertions, 0 deletions
diff --git a/files/de/web/javascript/reference/global_objects/string/indexof/index.html b/files/de/web/javascript/reference/global_objects/string/indexof/index.html new file mode 100644 index 0000000000..dc36abfae8 --- /dev/null +++ b/files/de/web/javascript/reference/global_objects/string/indexof/index.html @@ -0,0 +1,200 @@ +--- +title: String.prototype.indexOf() +slug: Web/JavaScript/Reference/Global_Objects/String/indexOf +tags: + - JavaScript + - Method + - Prototype + - Reference + - String +translation_of: Web/JavaScript/Reference/Global_Objects/String/indexOf +--- +<div>{{JSRef}}</div> + +<p>Die <strong><code>indexOf()</code></strong> Methode gibt den Index der Zeichenkette innerhalb des aufrufenden {{jsxref("Global_Objects/String", "String")}} Objekts des ersten Vorkommnis des angegebenen Wertes beginnend bei <code>fromIndex</code> zurück. Gibt -1 zurück, wenn der Wert nicht gefunden wurde.</p> + +<h2 id="Syntax" name="Syntax">Syntax</h2> + +<pre class="syntaxbox"><code><var>str</var>.indexOf(<var>searchValue</var>[, <var>fromIndex</var>]</code>)</pre> + +<h3 id="Parameters" name="Parameters">Parameter</h3> + +<dl> + <dt><code>searchValue</code></dt> + <dd>Ein String der den zu suchenden Wert repräsentiert.</dd> + <dt><code>fromIndex</code> {{optional_inline}}</dt> + <dd>Der Index, von dem angefangen wird vorwärts im String zu suchen. Der Standardwert ist 0, so dass der ganze String durchsucht wird. Wenn <code>fromIndex < 0</code> ist, wird der ganze String durchsucht. Wenn <code>fromIndex >= str.length</code> ist, wird der String nicht durchsucht und -1 wird zurückgegeben. Die Ausnahme ist, wenn für <code>searchValue</code> ein leeren String eingesetzt wird, dann wird <code>str.length</code> zurückgegeben.</dd> +</dl> + +<h3 id="Rückgabewert">Rückgabewert</h3> + +<p>Den Index des ersten Vorkommens des gesuchten Wertes; <strong>-1</strong> wenn der Wert nicht gefunden wurde.</p> + +<h2 id="Description" name="Description">Beschreibung</h2> + +<p>Die Zeichen in einem String sind von links nach rechts nummeriert. Der Index des ersten Zeichens ist 0, und der Index des letzten Zeichens eines Strings mit Namen <code>stringName</code><code> </code>ist <code>stringName.length - 1</code>.</p> + +<pre class="brush: js">'Blue Whale'.indexOf('Blue'); // returns 0 +'Blue Whale'.indexOf('Blute'); // returns -1 +'Blue Whale'.indexOf('Whale', 0); // returns 5 +'Blue Whale'.indexOf('Whale', 5); // returns 5 +'Blue Whale'.indexOf('', 9); // returns 9 +'Blue Whale'.indexOf('', 10); // returns 10 +'Blue Whale'.indexOf('', 11); // returns 10 +</pre> + +<h3 id="Case-sensitivity" name="Case-sensitivity">Groß- und Kleinschreibung</h3> + +<p>Die <code>indexOf()</code> Methode unterscheidet zwischen Groß- und Kleinschreibung. Zum Beispiel gibt die folgende Zeile -1 zurück:</p> + +<pre class="brush: js">'Blue Whale'.indexOf('blue'); // returns -1 +</pre> + +<h3 id="Vorkommnisse_prüfen">Vorkommnisse prüfen</h3> + +<p>Zu beachten ist, dass '0' nicht zu <code>true</code> und '-1' nicht zu <code>false</code> ausgewertet wird. Deswegen ist der korrekte weg zum überprüfen, ob ein String in einem anderen String existiert, der folgende:</p> + +<pre class="brush: js">'Blue Whale'.indexOf('Blue') != -1; // true +'Blue Whale'.indexOf('Bloe') != -1; // false +</pre> + +<h2 id="Examples" name="Examples">Beispiele</h2> + +<h3 id="Example:_Using_indexOf_and_lastIndexOf" name="Example:_Using_indexOf_and_lastIndexOf">Einsatz von <code>indexOf()</code> <code>und lastIndexOf()</code></h3> + +<p>Die folgenden Beispiele benutzen <code>indexOf()</code> und {{jsxref("String.prototype.lastIndexOf()", "lastIndexOf()")}}, um Werte im String <code>"Brave new world"</code> zu finden.</p> + +<pre class="brush: js">var anyString = 'Brave new world'; + +console.log('The index of the first w from the beginning is ' + anyString.indexOf('w')); +// Displays 8 +console.log('The index of the first w from the end is ' + anyString.lastIndexOf('w')); +// Displays 10 + +console.log('The index of "new" from the beginning is ' + anyString.indexOf('new')); +// Displays 6 +console.log('The index of "new" from the end is ' + anyString.lastIndexOf('new')); +// Displays 6 +</pre> + +<h3 id="Case-sensitivity" name="Case-sensitivity"><code>indexOf()</code> und Groß- und Kleinschreibung</h3> + +<p>Das folgende Beispiel definiert zwei Strings. Die Variablen enthalten den selben String, außer dass der zweite String Großbuchstaben enthält. Die erste {{domxref("console.log()")}} Methode zeigt 19. Da die <code>indexOf()</code> Methode Groß- und Kleinschreibung unterscheidet, wird der String<code>"cheddar"</code> im String <code>myCapString</code> nicht gefunden. Deshalb gibt die zweite <code>console.log()</code> Methode -1 zurück.</p> + +<pre class="brush: js">var myString = 'brie, pepper jack, cheddar'; +var myCapString = 'Brie, Pepper Jack, Cheddar'; + +console.log('myString.indexOf("cheddar") is ' + myString.indexOf('cheddar')); +// Displays 19 +console.log('myCapString.indexOf("cheddar") is ' + myCapString.indexOf('cheddar')); +// Displays -1 +</pre> + +<h3 id="Example:_Using_indexOf_to_count_occurrences_of_a_letter_in_a_string" name="Example:_Using_indexOf_to_count_occurrences_of_a_letter_in_a_string">Einsatz von <code>indexOf()</code>, um das Vorkommen eines Buchstaben in einem String zu zählen</h3> + +<p>Das folgende Beispiel setzt <code>count</code> auf die Anzahl der Vorkommnisse des Buchstaben <code>e</code> in dem String <code>str</code>:</p> + +<pre class="brush: js">var str = 'To be, or not to be, that is the question.'; +var count = 0; +var pos = str.indexOf('e'); + +while (pos != -1) { + count++; + pos = str.indexOf('e', pos + 1); +} + +console.log(count); // displays 4 +</pre> + +<h2 id="Spezifikationen">Spezifikationen</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">Spezifikation</th> + <th scope="col">Status</th> + <th scope="col">Kommentar</th> + </tr> + <tr> + <td>{{SpecName('ES1')}}</td> + <td>{{Spec2('ES1')}}</td> + <td>Initiale Definition.</td> + </tr> + <tr> + <td>{{SpecName('ES5.1', '#sec-15.5.4.7', 'String.prototype.indexOf')}}</td> + <td>{{Spec2('ES5.1')}}</td> + <td> </td> + </tr> + <tr> + <td>{{SpecName('ES6', '#sec-string.prototype.indexof', 'String.prototype.indexOf')}}</td> + <td>{{Spec2('ES6')}}</td> + <td> </td> + </tr> + <tr> + <td>{{SpecName('ESDraft', '#sec-string.prototype.indexof', 'String.prototype.indexOf')}}</td> + <td>{{Spec2('ESDraft')}}</td> + <td> </td> + </tr> + </tbody> +</table> + +<h2 id="Browserkompatibilität">Browserkompatibilität</h2> + +<div>{{CompatibilityTable}}</div> + +<div id="compat-desktop"> +<table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Chrome</th> + <th>Firefox (Gecko)</th> + <th>Internet Explorer</th> + <th>Opera</th> + <th>Safari</th> + </tr> + <tr> + <td>Basic support</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + </tr> + </tbody> +</table> +</div> + +<div id="compat-mobile"> +<table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Android</th> + <th>Chrome for Android</th> + <th>Firefox Mobile (Gecko)</th> + <th>IE Mobile</th> + <th>Opera Mobile</th> + <th>Safari Mobile</th> + </tr> + <tr> + <td>Basic support</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + </tr> + </tbody> +</table> +</div> + +<h2 id="See_also" name="See_also">Siehe auch</h2> + +<ul> + <li>{{jsxref("String.prototype.charAt()")}}</li> + <li>{{jsxref("String.prototype.lastIndexOf()")}}</li> + <li>{{jsxref("String.prototype.split()")}}</li> + <li>{{jsxref("Array.prototype.indexOf()")}}</li> +</ul> |