logo

Java String indexOf()

EnJava, String indexOf()El mètode retorna la posició de la primera ocurrència del caràcter o cadena especificats en una cadena especificada.

Variants del mètode indexOf().

N'hi ha quatre Les variants del mètode indexOf() s'esmenten a continuació:

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

1. int indexOf()

Aquest mètode torna el índex dins d'aquesta cadena de la primer l'aparició del caràcter especificat o -1, si el caràcter no apareix.



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

A continuació es mostra la implementació del mètode anterior

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

java end for bucle
>

>

Sortida

Found g first at position : 11>

2. int indexOf(char ch, int strt)

Aquest mètode torna l'índex dins d'aquesta cadena de la primer l'aparició del caràcter especificat, començant la cerca a l'índex especificat o -1, si el caràcter no apareix.

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

Exemple del mètode anterior:

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

>

peus contra peus

>

Sortida

Found g after 13th index at position : 19>

3. int indexOf(String str)

Aquest mètode torna l'índex dins d'aquesta cadena de la primer aparició de l'especificat subcadena . Si no es produeix com a subcadena, es retorna -1.

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

Exemple del mètode anterior:

Java




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

>

>

Sortida

Found geeks starting at position : 11>

4. int indexOf(String str, int str)

Aquest mètode torna l'índex dins d'aquesta cadena de la primer aparició de l'especificat subcadena , començant a l'especificat índex . Si no es produeix, es retorna -1.

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

Java




chmod 755

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

>

>

Sortida

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

Algunes aplicacions relacionades

Esbrinar si un caràcter determinat (potser qualsevol cosa en majúscules o minúscules) és una vocal o una consonant.

La implementació es presenta a continuació:

Java


gimp com anul·lar la selecció



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

>

>

Sortida

Vowel>