diff options
author | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:42:17 -0500 |
---|---|---|
committer | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:42:17 -0500 |
commit | da78a9e329e272dedb2400b79a3bdeebff387d47 (patch) | |
tree | e6ef8aa7c43556f55ddfe031a01cf0a8fa271bfe /files/id/web/javascript/reference/global_objects/string/indexof/index.html | |
parent | 1109132f09d75da9a28b649c7677bb6ce07c40c0 (diff) | |
download | translated-content-da78a9e329e272dedb2400b79a3bdeebff387d47.tar.gz translated-content-da78a9e329e272dedb2400b79a3bdeebff387d47.tar.bz2 translated-content-da78a9e329e272dedb2400b79a3bdeebff387d47.zip |
initial commit
Diffstat (limited to 'files/id/web/javascript/reference/global_objects/string/indexof/index.html')
-rw-r--r-- | files/id/web/javascript/reference/global_objects/string/indexof/index.html | 190 |
1 files changed, 190 insertions, 0 deletions
diff --git a/files/id/web/javascript/reference/global_objects/string/indexof/index.html b/files/id/web/javascript/reference/global_objects/string/indexof/index.html new file mode 100644 index 0000000000..3b7f7d0965 --- /dev/null +++ b/files/id/web/javascript/reference/global_objects/string/indexof/index.html @@ -0,0 +1,190 @@ +--- +title: String.prototype.indexOf() +slug: Web/JavaScript/Reference/Global_Objects/String/indexOf +translation_of: Web/JavaScript/Reference/Global_Objects/String/indexOf +--- +<div>{{JSRef}}</div> + +<p><strong><code>indexOf()</code></strong> method mengembalikan sebuah <em>index</em> saat memanggil objek {{jsxref("String")}} pertama kali dengan <em>value </em>yang di tentukan, bermula dari pencarian pada <em><code>fromIndex</code></em>. Dan pencarian ini akan mengembalikan index dari karakter pada String, dan akan mengembalikan <em>-1</em> ketika pencarian <em>indexOf </em>ini tak menemukan karakter yang cocok/ value yang sesuai.</p> + +<h2 id="Syntax">Syntax</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>Sebuah string yang me-representasikan dari nilai yang ingin di cari.</dd> + <dt><code>fromIndex</code> {{optional_inline}}</dt> + <dd>Index bermulai dari awal hingga bergerak ke depan sepanjang string. Index bisa berupa bilangan bulat atau apa pun. Nilai standarnya adalah <code>0</code>, jadi seluruh index dari array akan dicari. Jika <code>fromIndex < 0</code> seluruh string dicari. Jika <code>fromIndex >= str.length</code>, string tidak dicari dan <code>-1</code> sebagai kembaliannya. Kecuali kalau <code>searchValue</code> adalah string kosong, maka <code>str.length</code> sebagai kembaliannya.</dd> +</dl> + +<h2 id="Deskripsi">Deskripsi</h2> + +<p>Karakter dalam sebuah <em>string</em> di indeks berurutan dari kiri ke kanan. Index pada karakter pertama yaitu 0, dan index pada karakter terakhir dalam sebuah <em>String </em>di sebut <em>stringName </em>is <code>stringName.length - 1</code>.</p> + +<pre class="brush: js">'Blue Whale'.indexOf('Blue'); // mengembalikan 0 +'Blue Whale'.indexOf('Blute'); // mengembalikan -1 +'Blue Whale'.indexOf('Whale', 0); // mengembalikan 5 +'Blue Whale'.indexOf('Whale', 5); // mengembalikan 5 +'Blue Whale'.indexOf('', 9); // mengembalikan 9 +'Blue Whale'.indexOf('', 10); // mengembalikan 10 +'Blue Whale'.indexOf('', 11); // mengembalikan 10 +</pre> + +<h3 id="Case-sensitivity">Case-sensitivity</h3> + +<p>The <code>indexOf()</code> method sangat <em>case sensitive</em>. Sebagai contoh, <em>expression</em> berikut ini mengembalikan -1:</p> + +<pre class="brush: js">'Blue Whale'.indexOf('blue'); // mengembalikan -1 +</pre> + +<h3 id="Checking_occurrences_Memeriksa_suatu_kejadian">Checking occurrences/ Memeriksa suatu kejadian</h3> + +<p>Catat bahwa '0' tak bernilai <em>true</em> dan '-1' bukan bernilali <em>f<code>alse</code></em>. Oleh karena-nya, ketika memeriksa apakah sebuah <em>String</em> Therefore, when checking if a specific string exists within another string the correct way to check would be:</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="Using_indexOf()_and_lastIndexOf()">Using <code>indexOf()</code> and <code>lastIndexOf()</code></h3> + +<p>The following example uses <code>indexOf()</code> and {{jsxref("String.prototype.lastIndexOf()", "lastIndexOf()")}} to locate values in the string <code>"Brave new world"</code>.</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')); +// logs 8 +console.log('The index of the last w from the beginning is ' + anyString.lastIndexOf('w')); +// logs 10 + +console.log('The index of "new" from the beginning is ' + anyString.indexOf('new')); +// logs 6 +console.log('The index of "new" from the end is ' + anyString.lastIndexOf('new')); +// logs 6 +</pre> + +<h3 id="indexOf()_and_case-sensitivity"><code>indexOf()</code> and case-sensitivity</h3> + +<p>The following example defines two string variables. The variables contain the same string except that the second string contains uppercase letters. The first {{domxref("console.log()")}} method displays 19. But because the <code>indexOf()</code> method is case sensitive, the string <code>"cheddar"</code> is not found in <code>myCapString</code>, so the second <code>console.log()</code> method displays -1.</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')); +// logs 19 +console.log('myCapString.indexOf("cheddar") is ' + myCapString.indexOf('cheddar')); +// logs -1 +</pre> + +<h3 id="Using_indexOf()_to_count_occurrences_of_a_letter_in_a_string">Using <code>indexOf()</code> to count occurrences of a letter in a string</h3> + +<p>The following example sets <code>count</code> to the number of occurrences of the letter <code>e</code> in the 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="Specifications">Specifications</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">Specification</th> + <th scope="col">Status</th> + <th scope="col">Comment</th> + </tr> + <tr> + <td>{{SpecName('ES1')}}</td> + <td>{{Spec2('ES1')}}</td> + <td>Initial 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="Browser_compatibility">Browser compatibility</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">See also</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> |