logo

Java String indexOf()

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

Varianty metody indexOf().

Existují čtyři varianty metody indexOf() jsou uvedeny níže:

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

1. int indexOf()

Tato metoda se vrací a index v tomto řetězci První výskyt zadaného znaku nebo -1, pokud se znak nevyskytuje.



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

Níže je uvedena implementace výše uvedené metody

Jáva




// 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'>));> >}> }>

seznam třídit pole
>

>

Výstup

Found g first at position : 11>

2. int indexOf(char ch, int strt)

Tato metoda se vrací index v tomto řetězci První výskyt zadaného znaku se zahájením hledání na zadaném indexu nebo -1, pokud se znak nevyskytuje.

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

Příklad výše uvedené metody:

Jáva




// 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>));> >}> }>

>

datové typy pokračování

>

Výstup

Found g after 13th index at position : 19>

3. int indexOf(String str)

Tato metoda se vrací index v tomto řetězci První výskyt zadaného podřetězec . Pokud se nevyskytuje jako podřetězec, je vráceno -1.

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

Příklad výše uvedené metody:

Jáva




// 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));> >}> }>

>

v.další java

>

Výstup

Found geeks starting at position : 11>

4. int indexOf(String str, int strt)

Tato metoda se vrací index v tomto řetězci První výskyt zadaného podřetězec , začínající při zadaném index . Pokud nenastane, vrátí se -1.

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

Jáva

převést řetězec na int v jazyce 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ýstup

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

Některé související aplikace

Zjištění, zda daný znak (třeba cokoliv s velkým nebo malým písmenem) je samohláska nebo souhláska.

Implementace je uvedena níže:

Jáva


weby, jako je bedpage



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ýstup

Vowel>