logo

Java String indexOf()

The Třída Java String indexOf() metoda vrací pozici prvního výskytu zadaného znaku nebo řetězce v zadaném řetězci.

Podpis

V Javě jsou čtyři přetížené metody indexOf(). Podpis metod indexOf() je uveden níže:

Ne.MetodaPopis
1int indexOf(int ch)Vrátí pozici indexu pro danou hodnotu znaku
2int indexOf(int ch, int fromIndex)Vrací pozici indexu pro danou hodnotu znaku a z indexu
3int indexOf(podřetězec řetězce)Vrátí pozici indexu pro daný podřetězec
4int indexOf(řetězec podřetězec, int zIndexu)Vrací pozici indexu pro daný podřetězec az indexu

Parametry

ch : Je to znaková hodnota, např. 'A'

z Indexu : Pozice indexu, odkud je vrácen index hodnoty znaku nebo podřetězce.

podřetězec : Podřetězec, který se má v tomto řetězci hledat.

Návraty

Index hledaného řetězce nebo znaku.

Interní implementace

 public int indexOf(int ch) { return indexOf(ch, 0); } 

Příklad metody Java String indexOf()

Název souboru: IndexOfExample.java

 public class IndexOfExample{ public static void main(String args[]){ String s1='this is index of example'; //passing substring int index1=s1.indexOf('is');//returns the index of is substring int index2=s1.indexOf('index');//returns the index of index substring System.out.println(index1+' '+index2);//2 8 //passing substring with from index int index3=s1.indexOf('is',4);//returns the index of is substring after 4th index System.out.println(index3);//5 i.e. the index of another is //passing char value int index4=s1.indexOf('s');//returns the index of s char value System.out.println(index4);//3 }} 
Otestujte to hned

Výstup:

 2 8 5 3 

Pozorujeme, že když je nalezen hledaný řetězec nebo znak, metoda vrací nezápornou hodnotu. Pokud řetězec nebo znak není nalezen, vrátí se -1. Tuto vlastnost můžeme použít k nalezení celkového počtu znaků přítomných v daném řetězci. Sledujte následující příklad.

Název souboru: IndexOfExample5.java

 public class IndexOfExample5 { // main method public static void main(String argvs[]) { String str = 'Welcome to JavaTpoint'; int count = 0; int startFrom = 0; for(; ;) { int index = str.indexOf('o', startFrom); if(index >= 0) { // match found. Hence, increment the count count = count + 1; // start looking after the searched index startFrom = index + 1; } else { // the value of index is - 1 here. Therefore, terminate the loop break; } } System.out.println('In the String: '+ str); System.out.println('The 'o' character has come '+ count + ' times'); } } 

Výstup:

 In the String: Welcome to JavaTpoint The 'o' character has come 3 times 

Java String indexOf(String substring) Příklad metody

Metoda bere podřetězec jako argument a vrací index prvního znaku podřetězce.

Název souboru: IndexOfExample2.java

 public class IndexOfExample2 { public static void main(String[] args) { String s1 = 'This is indexOf method'; // Passing Substring int index = s1.indexOf('method'); //Returns the index of this substring System.out.println('index of substring '+index); } } 
Otestujte to hned

Výstup:

 index of substring 16 

Java String indexOf(String substring, int fromIndex) Příklad metody

Metoda bere podřetězec a index jako argumenty a vrací index prvního znaku, který se vyskytuje po daném z Indexu .

Název souboru: IndexOfExample3.java

 public class IndexOfExample3 { public static void main(String[] args) { String s1 = 'This is indexOf method'; // Passing substring and index int index = s1.indexOf('method', 10); //Returns the index of this substring System.out.println('index of substring '+index); index = s1.indexOf('method', 20); // It returns -1 if substring does not found System.out.println('index of substring '+index); } } 
Otestujte to hned

Výstup:

 index of substring 16 index of substring -1 

Java String indexOf(int char, int fromIndex) Příklad metody

Metoda bere char a index jako argumenty a vrací index prvního znaku, který se vyskytuje po daném z Indexu .

Název souboru: IndexOfExample4.java

 public class IndexOfExample4 { public static void main(String[] args) { String s1 = 'This is indexOf method'; // Passing char and index from int index = s1.indexOf('e', 12); //Returns the index of this char System.out.println('index of char '+index); } } 
Otestujte to hned

Výstup:

 index of char 17