Dokumentacja języka JavaScript 1.5:Obiekty:String:indexOf
z Mozilla Developer Center, polskiego centrum programistów Mozilli.
UWAGA: Tłumaczenie tej strony nie zostało zakończone.
Może być ona niekompletna lub wymagać korekty.
Chcesz pomóc? | Dokończ tłumaczenie | Sprawdź ortografię | Więcej takich stron...
Spis treści |
[edytuj] Podsumowanie
Returns the index within the calling String object of the first occurrence of the specified value, starting the search at fromIndex, or -1 if the value is not found.
| Metoda obiektu: String | |
| Zaimplementowana w: | JavaScript 1.0, NES2.0 |
| Wersja ECMA: | ECMA-262 |
[edytuj] Składnia
indexOf(searchValue[, fromIndex])
[edytuj] Parametry
-
searchValue - A string representing the value to search for.
-
fromIndex - The location within the calling string to start the search from. It can be any integer between 0 and the length of the string. The default value is 0.
[edytuj] Opis
Characters in a string are indexed from left to right. The index of the first character is 0, and the index of the last character of a string called stringName is stringName.length - 1.
"Blue Whale".indexOf("Blue") // zwraca 0
"Blue Whale".indexOf("Blute") // zwraca -1
"Blue Whale".indexOf("Whale",0) // zwraca 5
"Blue Whale".indexOf("Whale",5) // zwraca 5
"Blue Whale".indexOf("",9) // zwraca 9
"Blue Whale".indexOf("",10) // zwraca 10
"Blue Whale".indexOf("",11) // zwraca 10
indexOf method is case sensitive. For example, the following expression returns -1:
"Blue Whale".indexOf("blue")
[edytuj] Przykłady
[edytuj] Przykład: Using indexOf and lastIndexOf
The following example uses indexOf and lastIndexOf to locate values in the string "Brave new world".
var anyString="Brave new world"
// Displays 8
document.write("<P>The index of the first w from the beginning is " +
anyString.indexOf("w"))
// Displays 10
document.write("<P>The index of the first w from the end is " +
anyString.lastIndexOf("w"))
// Displays 6
document.write("<P>The index of 'new' from the beginning is " +
anyString.indexOf("new"))
// Displays 6
document.write("<P>The index of 'new' from the end is " +
anyString.lastIndexOf("new"))
[edytuj] Przykład: 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 writeln method displays 19. But because the indexOf method is case sensitive, the string "cheddar" is not found in myCapString, so the second writeln method displays -1.
myString="brie, pepper jack, cheddar"
myCapString="Brie, Pepper Jack, Cheddar"
document.writeln('myString.indexOf("cheddar") is ' +
myString.indexOf("cheddar"))
document.writeln('<P>myCapString.indexOf("cheddar") is ' +
myCapString.indexOf("cheddar"))
[edytuj] Przykład: Using indexOf to count occurrences of a letter in a string
The following example sets count to the number of occurrences of the letter x in the string str:
count = 0;
pos = str.indexOf("x");
while ( pos != -1 ) {
count++;
pos = str.indexOf("x",pos+1);
}