String.prototype.indexOf()
indexOf()
method mengembalikan sebuah index saat memanggil objek String
pertama kali dengan value yang di tentukan, bermula dari pencarian pada fromIndex
. Dan pencarian ini akan mengembalikan index dari karakter pada String, dan akan mengembalikan -1 ketika pencarian indexOf ini tak menemukan karakter yang cocok/ value yang sesuai.
Syntax
str.indexOf(searchValue[, fromIndex]
)
Parameters
searchValue
- Sebuah string yang me-representasikan dari nilai yang ingin di cari.
fromIndex
Optional- Index bermulai dari awal hingga bergerak ke depan sepanjang string. Index bisa berupa bilangan bulat atau apa pun. Nilai standarnya adalah
0
, jadi seluruh index dari array akan dicari. JikafromIndex < 0
seluruh string dicari. JikafromIndex >= str.length
, string tidak dicari dan-1
sebagai kembaliannya. Kecuali kalausearchValue
adalah string kosong, makastr.length
sebagai kembaliannya.
Deskripsi
Karakter dalam sebuah string di indeks berurutan dari kiri ke kanan. Index pada karakter pertama yaitu 0, dan index pada karakter terakhir dalam sebuah String di sebut stringName is stringName.length - 1
.
'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
Case-sensitivity
The indexOf()
method sangat case sensitive. Sebagai contoh, expression berikut ini mengembalikan -1:
'Blue Whale'.indexOf('blue'); // mengembalikan -1
Checking occurrences/ Memeriksa suatu kejadian
Catat bahwa '0' tak bernilai true dan '-1' bukan bernilali false
. Oleh karena-nya, ketika memeriksa apakah sebuah String Therefore, when checking if a specific string exists within another string the correct way to check would be:
'Blue Whale'.indexOf('Blue') !== -1; // true
'Blue Whale'.indexOf('Bloe') !== -1; // false
Examples
Using indexOf()
and lastIndexOf()
The following example uses indexOf()
and lastIndexOf()
(en-US) to locate values in the string "Brave new world"
.
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
indexOf()
and case-sensitivity
The following example defines two string variables. The variables contain the same string except that the second string contains uppercase letters. The first console.log()
(en-US) method displays 19. But because the indexOf()
method is case sensitive, the string "cheddar"
is not found in myCapString
, so the second console.log()
method displays -1.
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
Using indexOf()
to count occurrences of a letter in a string
The following example sets count
to the number of occurrences of the letter e
in the string str
:
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
Specifications
Specification | Status | Comment |
---|---|---|
ECMAScript 1st Edition (ECMA-262) | Standard | Initial definition. |
ECMAScript 5.1 (ECMA-262) The definition of 'String.prototype.indexOf' in that specification. |
Standard | |
ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'String.prototype.indexOf' in that specification. |
Standard | |
ECMAScript (ECMA-262) The definition of 'String.prototype.indexOf' in that specification. |
Living Standard |
Browser compatibility
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|
Basic support | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
Feature | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|
Basic support | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |