WJava, ciąg znaków IndexOf()Metoda zwraca pozycję pierwszego wystąpienia określonego znaku lub ciągu w określonym ciągu.
Warianty metody IndexOf().
Tam są cztery warianty metody IndexOf() są wymienione poniżej:
- int indeks()
- int indeksOf(char ch, int strt)
- int indeksOf(String str)
- int indeksOf(String str, int strt)
1. int indeks()
Ta metoda zwroty the indeks w tym ciągu Pierwszy wystąpienie określonego znaku lub -1, jeśli znak nie występuje.
Syntax: int indexOf(char ch ) Parameters: ch : a character.>
Poniżej implementacja powyższej metody
Jawa
// 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'> ));> > }> }> |
różnica między firmą a firmą
>
>Wyjście
Found g first at position : 11>
2. int indeksOf(char ch, int strt)
Ta metoda zwroty indeks w tym ciągu znaków Pierwszy wystąpienie określonego znaku, rozpoczęcie wyszukiwania od podanego indeksu lub -1, jeśli znak nie występuje.
Syntax: int indexOf(char ch, int strt) Parameters: ch :a character. strt : the index to start the search from.>
Przykład powyższej metody:
Jawa
// 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 in, aby podwoić
>Wyjście
Found g after 13th index at position : 19>
3. int indeksOf(String str)
Ta metoda zwroty indeks w tym ciągu znaków Pierwszy wystąpienie określonego podciąg . Jeśli nie występuje jako podciąg, zwracane jest -1.
Syntax: int indexOf(String str) Parameters: str : a string.>
Przykład powyższej metody:
Jawa
// 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));> > }> }> |
sortowanie przez wstawianie w Javie
>
>
rzutuj ciąg jako int JavaWyjście
Found geeks starting at position : 11>
4. int indeksOf(String str, int strt)
Ta metoda zwroty indeks w tym ciągu znaków Pierwszy wystąpienie określonego podciąg , startowy w podanym indeks . Jeśli tak się nie stanie, zwracane jest -1.
Syntax: int indexOf(String str, int strt) Parameters: strt : the index to start the search from. str : a string.>
Jawa
// 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> ));> > }> }> |
>
>Wyjście
Found geeks(after 14th index) starting at position : 19>
Niektóre powiązane aplikacje
Sprawdzenie, czy dany znak (może cokolwiek pisanego dużą lub małą literą) jest samogłoską czy spółgłoską.
Poniżej znajduje się realizacja:
Jawa
przekonwertuj ciąg na znak
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'> );> > }> }> |
>
>Wyjście
Vowel>