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>De <strong><code>indexOf()</code></strong> methode geeft de positie van het eerste voorval van <code>searchValue</code> binnen het {{jsxref("String")}} object waarop het wordt aangeroepen, waarbij begonnen wordt met zoeken vanaf <code>fromIndex</code>. Geeft -1 terug als geen voorvallen van <code>searchValue </code>gevonden worden.</p>
<h2 id="Syntaxis">Syntaxis</h2>
<pre class="syntaxbox"><code><var>str</var>.indexOf(<var>searchValue</var>[, <var>fromIndex</var>]</code>)</pre>
<h3 id="Parameters">Parameters</h3>
<dl>
<dt><code>searchValue</code></dt>
<dd>De string om naar te zoeken.</dd>
<dt><code>fromIndex</code> {{optional_inline}}</dt>
<dd>De index vanaf waar gezocht moet worden binnen de string. Dit kan elke integer zijn. De standaard waarde is <code>0</code>, waardoor de hele string wordt doorzocht. Als <code>fromIndex < 0</code> is wordt de hele string doorzocht. Als <code>fromIndex >= str.length</code> is wordt de string niet doorzocht en wordt -1 teruggegeven. (behalve als <code>searchValue</code> een lege string is, dan wordt <code>str.length</code> teruggegeven)</dd>
</dl>
<h3 id="Return_waarde">Return waarde</h3>
<p>De index waarop de gespecificeerde waarde het eerst voorkomt in de string; <strong>-1</strong> als die niet gevonden wordt.</p>
<h2 id="Beschrijving">Beschrijving</h2>
<p>Karakters in een string zijn geïndexeerd van links naar rechts. De index van het eerste karakter is 0 en de index van het laatste karakter van een string genaamd <code>stringName</code> is <code>stringName.length - 1</code>.</p>
<pre class="brush: js">'Blue Whale'.indexOf('Blue'); // geeft 0 terug
'Blue Whale'.indexOf('Blute'); // geeft -1 terug
'Blue Whale'.indexOf('Whale', 0); // geeft 5 terug
'Blue Whale'.indexOf('Whale', 5); // geeft 5 terug
'Blue Whale'.indexOf('', 9); // geeft 9 terug
'Blue Whale'.indexOf('', 10); // geeft 10 terug
'Blue Whale'.indexOf('', 11); // geeft 11 terug
</pre>
<h3 id="Hoofdlettergevoeligheid">Hoofdlettergevoeligheid</h3>
<p>De <code>indexOf()</code> methode is hoofdlettergevoelig. Het volgende voorbeeld geeft <code>-1</code> terug:</p>
<pre class="brush: js">'Blue Whale'.indexOf('blue'); // geeft -1 terug
</pre>
<h3 id="Voorvallen_controleren">Voorvallen controleren</h3>
<p>Onthoudt dat '0' niet vertaalt naar <code>true</code> en '-1' niet vertaalt naar <code>false</code>. Hierdoor moet op de volgende manier gekeken worden of een string binnen een andere string zit:</p>
<pre class="brush: js">'Blue Whale'.indexOf('Blue') !== -1; // true
'Blue Whale'.indexOf('Bloe') !== -1; // false
</pre>
<h2 id="Examples">Examples</h2>
<h3 id="indexOf()_en_lastIndexOf()_gebruiken"><code>indexOf()</code> en <code>lastIndexOf() gebruiken</code></h3>
<p>Het volgende voorbeeld gebruikt <code>indexOf()</code> en {{jsxref("String.prototype.lastIndexOf()", "lastIndexOf()")}} om waardes binnen de string <code>"Brave new world" </code>te vinden.</p>
<pre class="brush: js">var anyString = 'Brave new world';
console.log('De index van de eerste w vanaf het begin is ' + anyString.indexOf('w'));
// logs 8
console.log('De index van de eerste w vanaf het begin is ' + anyString.lastIndexOf('w'));
// logs 10
console.log('De index van "new" vanaf het begin is ' + anyString.indexOf('new'));
// logs 6
console.log('De index van "new" vanaf het eind is ' + anyString.lastIndexOf('new'));
// logs 6
</pre>
<h3 id="indexOf()_en_hoofdlettergevoeligheid"><code>indexOf()</code> en hoofdlettergevoeligheid</h3>
<p>Het volgende voorbeeld legt twee string variabelen vast. Deze variabelen bevatten dezelfde string, behalve dat de tweede string hoofdletters bevat. De eerste {{domxref("console.log()")}} methode geeft <code>19</code> terug. Omdat de <code>indexOf()</code> methode hoofdlettergevoelig is, wordt de string <code>"cheddar"</code> niet gevonden in <code>myCapString</code>, dus de tweede <code>console.log()</code> methode geeft <code>-1</code> terug.</p>
<pre class="brush: js">var myString = 'brie, pepper jack, cheddar';
var myCapString = 'Brie, Pepper Jack, Cheddar';
console.log('myString.indexOf("cheddar") geeft ' + myString.indexOf('cheddar'));
// geeft 19
console.log('myCapString.indexOf("cheddar") geeft ' + myCapString.indexOf('cheddar'));
// geeft -1
</pre>
<h3 id="indexOf()_gebruiken_om_voorvallen_van_een_letter_in_een_string_te_tellen"><code>indexOf()</code> gebruiken om voorvallen van een letter in een string te tellen</h3>
<p>In het volgende voorbeeld wordt in <code>count</code> de hoeveelheid voorvallen van <code>e</code> in de string <code>str</code> bijgehouden:</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); // geeft 4
</pre>
<h2 id="Specificaties">Specificaties</h2>
<table class="standard-table">
<tbody>
<tr>
<th scope="col">Specificatie</th>
<th scope="col">Status</th>
<th scope="col">Opmerking</th>
</tr>
<tr>
<td>{{SpecName('ES1')}}</td>
<td>{{Spec2('ES1')}}</td>
<td>Eerste definitie.</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="Browser_compatibiliteit">Browser compatibiliteit</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="Zie_ook">Zie ook</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>
|