Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Applications of String indexOf() Method in Java

  • Last Updated : 23 Sep, 2021

There are four variants of indexOf() method.

  1. 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.
  2. indexOf(char ch, int start ): 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.
  3. 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.
  4. indexOf(String str, int start): 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. 

Now let us come up with the applications of indexOf() method in java of which most frequently are listed below as follows: 

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

  1. To know whether a character is vowel or consonant.
  2. To count the occurrences of any character in a string.
  3. To know whether a character is present in String or not.
  4. To find whether a substring is present in String or not.
  5. To find whether an input is Digit or Letter or Special Character.

Now let us do discuss each of these applications by supporting them with the help of clean java programs 

Application 1



We will check that character is present in a pre-defined string of vowels. If it is present then it is a vowel else consonant.

Example 

Java




class Vowels
{
        // function to check if the passed
        // character is a vowel
    public static boolean vowel(char c)
    {
        return "aeiou".indexOf(Character.toLowerCase(c))>=0;
    }
  
        // Driver program
    public static void main(String[] args)
    {
        boolean isVowel = vowel('z');
          
                // Printing the output
                if(isVowel)
            System.out.println("Vowel");
        else
            System.out.println("Consonant");
    }
}
Output
Consonant

Application 2: To count the occurrences of any character in a string.

In this program, we will check that character is present in a string. If it is present then we will increment the counter and again execute the indexOf() function until the index is NOT FOUND.

Example 

Java




import java.io.*;
  
class GFG {
    public static void main (String[] args) {
        String s="GeeksForGeeks";
      int count=0;
        
      for(int i=0;i<s.length();i++)
      {
      i=s.indexOf('e',i);
        if(i<0)
              break;
        count++;
      }
      System.out.println("Count is "+ count);
    }
}
Output
Count is 4

Application 3: To know whether a character is present in String or not. 



In this program, we will check that character is present in a string.

Example

Java




import java.io.*;
  
class GFG {
    public static void main (String[] args) {
        String s="GeeksForGeeks";
      System.out.println(s.indexOf('m')<0?"Character not found":"Character found");
    }
}
Output
Character not found

Application 4: To find whether a substring is present in String or not. 

In this program, we will check that character is present in a string.

Example

Java




public class Geeks {
public static void main(String args[])
    {
  
        // Initialising string
        String Str = "Welcome to geeksforgeeks";
  
        // Initialising search string
        String subst = "geeks";
  
        System.out.println(Str.indexOf(subst)>=0?"Substring found at "+Str.indexOf(subst):"Substring not found");
}
}
Output
Substring found at 11

Application 5: To find whether an input is Digit or Letter or Special Character.

In this program, we will check that character is present in a pre-defined set of strings.

Example

Java




class Geek
{
    public static void check(char c)
    {
        if("0123456789".indexOf(c)>=0)
        {
          System.out.print("It is a digit\n");
        }
      else if("[abcdefghijklmnopqrstuvwxyz]".indexOf(Character.toLowerCase(c))>=0)
      {
        System.out.print("It is a Alphabet\n");
      }
      else{
        System.out.print("It is a Special Character\n");
      }
    }
   
  
        // Driver program
    public static void main(String[] args)
    {
      check('1');
      check('a');
      check('@');
        
    }
}
Output
It is a digit
It is a Alphabet
It is a Special Character



My Personal Notes arrow_drop_up
Recommended Articles
Page :