InJava, String indexOf()Die Methode gibt die Position des ersten Vorkommens des angegebenen Zeichens oder der angegebenen Zeichenfolge in einer angegebenen Zeichenfolge zurück.
Varianten der indexOf()-Methode
Es gibt vier Nachfolgend werden Varianten der indexOf()-Methode aufgeführt:
- int indexOf()
- int indexOf(char ch, int strt)
- int indexOf(String str)
- int indexOf(String str, int strt)
1. int indexOf()
Diese Methode kehrt zurück Die Index innerhalb dieser Zeichenfolge des Erste Vorkommen des angegebenen Zeichens oder -1, wenn das Zeichen nicht vorkommt.
Syntax: int indexOf(char ch ) Parameters: ch : a character.>
Nachfolgend finden Sie die Implementierung der obigen 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'> ));> > }> }> |
Mockito wann immer
>
>Ausgabe
Found g first at position : 11>
2. int indexOf(char ch, int strt)
Diese Methode kehrt zurück der Index innerhalb dieser Zeichenfolge des Erste Vorkommen des angegebenen Zeichens, Start der Suche am angegebenen Index oder -1, wenn das Zeichen nicht vorkommt.
Syntax: int indexOf(char ch, int strt) Parameters: ch :a character. strt : the index to start the search from.>
Beispiel für die obige 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> ));> > }> }> |
>
Laden Sie YouTube-Videos mit VLC herunter
>Ausgabe
Found g after 13th index at position : 19>
3. int indexOf(String str)
Diese Methode kehrt zurück der Index innerhalb dieser Zeichenfolge des Erste Auftreten des angegebenen Teilzeichenfolge . Wenn es nicht als Teilzeichenfolge auftritt, wird -1 zurückgegeben.
Syntax: int indexOf(String str) Parameters: str : a string.>
Beispiel für die obige Methode:
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));> > }> }> |
Vergleich von Löwe und Tiger
>
>Ausgabe
Found geeks starting at position : 11>
4. int indexOf(String str, int strt)
Diese Methode kehrt zurück der Index innerhalb dieser Zeichenfolge des Erste Auftreten des angegebenen Teilzeichenfolge , beginnend am angegebenen Index . Wenn dies nicht der Fall ist, wird -1 zurückgegeben.
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> ));> > }> }> |
ReactJS-Karte
>
>Ausgabe
Found geeks(after 14th index) starting at position : 19>
Einige verwandte Anwendungen
Herausfinden, ob ein bestimmtes Zeichen (vielleicht alles in Groß- oder Kleinschreibung) ein Vokal oder Konsonant ist.
Die Implementierung ist unten angegeben:
Java
int zum Stringen 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'> );> > }> }> |
>
>Ausgabe
Vowel>