Stringa Java indiceOf()

In Java, stringa indiceDi() Il metodo restituisce la posizione della prima occorrenza del carattere o della stringa specificata in una stringa specificata.

Varianti del metodo indexOf()

Ci sono quattro le varianti del metodo indexOf() sono menzionate di seguito:

  • int indiceDi()
  • int indiceOf(char ch, int strt)
  • int indiceOf(String str)
  • int indiceOf(String str, int strt)

1. int indiceDi()

Questo metodo ritorna IL indice all'interno di questa stringa di Primo occorrenza del carattere specificato o -1, se il carattere non ricorre.

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

Di seguito è riportata l'implementazione del metodo sopra

Giava




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

Produzione

Found g first at position : 11 

2. int indiceOf(char ch, int strt)

Questo metodo ritorna l'indice all'interno di questa stringa del Primo occorrenza del carattere specificato, avviando la ricerca dall'indice specificato o -1, se il carattere non ricorre.

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

Esempio del metodo sopra:

Giava




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

Produzione

Found g after 13th index at position : 19 

3. int indiceOf(String str)

Questo metodo ritorna l'indice all'interno di questa stringa del Primo occorrenza di quanto specificato sottostringa . Se non si presenta come sottostringa, viene restituito -1.

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

Esempio del metodo sopra:

Giava




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

Produzione

Found geeks starting at position : 11 

4. int indiceOf(String str, int strt)

Questo metodo ritorna l'indice all'interno di questa stringa del Primo occorrenza di quanto specificato sottostringa , di partenza al specificato indice . Se ciò non si verifica, viene restituito -1.

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

Giava




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

Produzione

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

Alcune applicazioni correlate

Scoprire se un dato carattere (magari qualsiasi cosa in maiuscolo o minuscolo) è una vocale o una consonante.

L'implementazione è riportata di seguito:

Giava




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

Produzione

Vowel