Java String indexOf()

There are four variants of indexOf() method. This article depicts about all of them, as follows:
1.String 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:
public int indexOf(int ch )
Parameters:
ch : a character.
filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

Found g first at position : 11

2. String 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:
public int indexOf(int ch, int strt)
Parameters:
ch :a character.
strt : the index to start the search from.

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

Found g after 13th index at position : 19

3.String 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.
filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

Found geeks starting at position : 11

4.String 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.
filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 aftr 14th position
        // prints 19
        System.out.print("Found geeks(after 14th index) starting at position : ");
        System.out.print(Str.indexOf(subst, 14));
    }
}

chevron_right


Output:

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

Some related applications:

  • Finding out if a given character (maybe anything upper or lower case) is a vowel or consonant.

    Implementation is given below:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    class Vowels
    {
            // function to check if the passed  
            // character is a vovel
        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");
        }
    }
      
    // This code is contributed by debjitdbb

    chevron_right

    
    

    Output:

    Vowel
    

This article is contributed by Astha Tyagi. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up