logo

Java String indexOf()

InJava, reťazec indexOf()metóda vráti pozíciu prvého výskytu zadaného znaku alebo reťazca v zadanom reťazci.

Varianty metódy indexOf().

Existujú štyri varianty metódy indexOf() sú uvedené nižšie:

  • int indexOf()
  • int indexOf(char ch, int strt)
  • int indexOf(String str)
  • int indexOf(String str, int strt)

1. int indexOf()

Táto metóda sa vracia a index v rámci tohto reťazca najprv výskyt zadaného znaku alebo -1, ak sa znak nevyskytuje.



 Syntax: int indexOf(char ch ) Parameters: ch : a character.>

Nižšie je uvedená implementácia vyššie uvedenej metódy

Java




// Java code to demonstrate the working> // of String indexOf()> public> class> Index1 {> >public> static> void> main(String args[])> >{> >// Initialising String> >String gfg =>new> String(>'Welcome to geeksforgeeks'>);> >System.out.print(>'Found g first at position : '>);> >// Initial index of 'g' will print> >// prints 11> >System.out.println(gfg.indexOf(>'g'>));> >}> }>

zoznam triediť pole
>

>

Výkon

Found g first at position : 11>

2. int indexOf(char ch, int strt)

Táto metóda sa vracia index v tomto reťazci najprv výskyt zadaného znaku, spustenie vyhľadávania na zadanom indexe alebo -1, ak sa znak nevyskytuje.

 Syntax: int indexOf(char ch, int strt) Parameters: ch  :a character. strt : the index to start the search from.>

Príklad vyššie uvedenej metódy:

Java




// Java code to demonstrate the working> // of String indexOf(char ch, int strt)> public> class> Index2 {> >public> static> void> main(String args[])> >{> >// Initialising String> >String gfg =>new> String(>'Welcome to geeksforgeeks'>);> >System.out.print(> >'Found g after 13th index at position : '>);> >// 2nd index of 'g' will print> >// prints 19> >System.out.println(gfg.indexOf(>'g'>,>13>));> >}> }>

>

číslo palindrómu

>

Výkon

Found g after 13th index at position : 19>

3. int indexOf(String str)

Táto metóda sa vracia index v rámci tohto reťazca najprv výskyt špecifikovaného podreťazec . Ak sa nevyskytuje ako podreťazec, vráti sa -1.

 Syntax: int indexOf(String str) Parameters: str : a string.>

Príklad vyššie uvedenej metódy:

Java


reťazec na znak java



// Java code to demonstrate the working> // of String indexOf(String str)> public> class> Index3 {> >public> static> void> main(String args[])> >{> >// Initialising string> >String Str =>new> String(>'Welcome to geeksforgeeks'>);> >// Initialising search string> >String subst =>new> String(>'geeks'>);> >// print the index of initial character> >// of Substring> >// prints 11> >System.out.print(> >'Found geeks starting at position : '>);> >System.out.print(Str.indexOf(subst));> >}> }>

>

>

globálne premenné js
Výkon

Found geeks starting at position : 11>

4. int indexOf(String str, int strt)

Táto metóda sa vracia index v tomto reťazci najprv výskyt špecifikovaného podreťazec , počnúc pri určenom index . Ak sa tak nestane, vráti sa -1.

 Syntax: int indexOf(String str, int strt) Parameters: strt : the index to start the search from. str : a string.>

Java




// Java code to demonstrate the working> // of String indexOf(String str, int strt)> public> class> Index4 {> >public> static> void> main(String args[])> >{> >// Initialising string> >String Str =>new> String(>'Welcome to geeksforgeeks'>);> >// Initialising search string> >String subst =>new> String(>'geeks'>);> >// print the index of initial character> >// of Substring after 14th position> >// prints 19> >System.out.print(> >'Found geeks(after 14th index) starting at position : '>);> >System.out.print(Str.indexOf(subst,>14>));> >}> }>

>

>

Výkon

Found geeks(after 14th index) starting at position : 19>

Niektoré súvisiace aplikácie

Zistenie, či daný znak (možno čokoľvek veľké alebo malé) je samohláska alebo spoluhláska.

Implementácia je uvedená nižšie:

Java


arp - príkaz



class> Vowels {> >// function to check if the passed> >// character is a vowel> >public> static> boolean> vowel(>char> c)> >{> >return> 'aeiouAEIOU'>.indexOf(c)>=>0>;> >}> >// Driver program> >public> static> void> main(String[] args)> >{> >boolean> isVowel = vowel(>'a'>);> >// Printing the output> >if> (isVowel)> >System.out.println(>'Vowel'>);> >else> >System.out.println(>'Consonant'>);> >}> }>

>

>

Výkon

Vowel>