1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
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>
|