In Java, String indexOf() method returns the position of the first occurrence of the specified character or string in a specified string.
Variants of indexOf() Method
There are four variants of the indexOf() method are mentioned below:
- int indexOf()
- int indexOf(char ch, int strt)
- int indexOf(String str)
- int indexOf(String str, int strt)
1. int indexOf()
This method returns the index within this string of the first occurrence of the specified character or -1, if the character does not occur.
Syntax: int indexOf(char ch ) Parameters: ch : a character.
Below is the implementation of the above method
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 neveropen" ); Â
        System.out.print( "Found g first at position : " ); Â
        // Initial index of 'g' will print         // prints 11         System.out.println(gfg.indexOf( 'g' ));     } } |
Found g first at position : 11
2. int indexOf(char ch, int strt)Â
This method returns the index within this string of the first occurrence of the specified character, starting the search at the specified index or -1, if the character does not occur.
Syntax: int indexOf(char ch, int strt) Parameters: ch :a character. strt : the index to start the search from.
Example of the above method:
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 neveropen" ); Â
        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 ));     } } |
Found g after 13th index at position : 19
3. int indexOf(String str)Â
This method returns the index within this string of the first occurrence of the specified substring. If it does not occur as a substring, -1 is returned.
Syntax: int indexOf(String str) Parameters: str : a string.
Example of the above method:
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 neveropen" ); Â
        // 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));     } } |
Found geeks starting at position : 11
4. int indexOf(String str, int strt)Â
This method returns the index within this string of the first occurrence of the specified substring, starting at the specified index. If it does not occur, -1 is returned.Â
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 neveropen" ); Â
        // 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 ));     } } |
Found geeks(after 14th index) starting at position : 19
Some Related Applications
Finding out if a given character (maybe anything in upper or lower case) is a vowel or consonant.Â
Implementation is given below:Â
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" );     } } |
Vowel
[…] from C++ STL, the index method in Python, the indexOf method in Java, the indexOf method in JavaScript are some inbuilt functions in the libraries of […]