InJava, StringindexOf()methode retourneert de positie van de eerste keer dat het opgegeven teken of de opgegeven tekenreeks in een opgegeven tekenreeks voorkomt.
Varianten van de indexOf()-methode
Er zijn vier varianten van de indexOf()-methode worden hieronder vermeld:
- int indexOf()
- int indexOf(char ch, int strt)
- int indexOf(Stringstr)
- int indexOf(Stringstr, intstrt)
1. int indexOf()
Deze methode geeft terug de inhoudsopgave binnen deze reeks van de Eerst voorkomen van het opgegeven teken of -1, als het teken niet voorkomt.
Syntax: int indexOf(char ch ) Parameters: ch : a character.>
Hieronder vindt u de implementatie van de bovenstaande methode
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'>));> >}> }> |
Unix top-commando
>
>Uitvoer
Found g first at position : 11>
2. int indexOf(char ch, int strt)
Deze methode geeft terug de index binnen deze string van de Eerst voorkomen van het opgegeven teken, waarbij de zoekopdracht begint bij de opgegeven index of -1, als het teken niet voorkomt.
Syntax: int indexOf(char ch, int strt) Parameters: ch :a character. strt : the index to start the search from.>
Voorbeeld van de bovenstaande methode:
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>));> >}> }> |
>
java len van array
>Uitvoer
Found g after 13th index at position : 19>
3. int indexOf(Stringstr)
Deze methode geeft terug de index binnen deze string van de Eerst optreden van het opgegeven subtekenreeks . Als deze niet als subtekenreeks voorkomt, wordt -1 geretourneerd.
Syntax: int indexOf(String str) Parameters: str : a string.>
Voorbeeld van de bovenstaande methode:
Java
datamining
// 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));> >}> }> |
>
>
tekenreeks vervangt alle JavaUitvoer
Found geeks starting at position : 11>
4. int indexOf(String str, int strt)
Deze methode geeft terug de index binnen deze string van de Eerst optreden van het opgegeven subtekenreeks , beginnend bij de opgegeven inhoudsopgave . Als dit niet gebeurt, wordt -1 geretourneerd.
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>));> >}> }> |
>
>Uitvoer
Found geeks(after 14th index) starting at position : 19>
Enkele gerelateerde toepassingen
Uitzoeken of een bepaald teken (misschien iets in hoofdletters of kleine letters) een klinker of medeklinker is.
Hieronder vindt u de implementatie:
Java
tekenreeks toevoegen in Java
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'>);> >}> }> |
>
>Uitvoer
Vowel>