Applications of String indexOf() Method in Java - GeeksforGeeks

23 Sep, 2021
There are four variants of indexOf() method.
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.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.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.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. 
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.
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.
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.
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: 
To know whether a character is vowel or consonant.To count the occurrences of any character in a string.To know whether a character is present in String or not.To find whether a substring is present in String or not.To find whether an input is Digit or Letter or Special Character.
To know whether a character is vowel or consonant.
To count the occurrences of any character in a string.
To know whether a character is present in String or not.
To find whether a substring is present in String or not.
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");    }}
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);    }}
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");    }}
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");}}
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('@');            }}
It is a digit
It is a Alphabet
It is a Special Character
Blogathon-2021
Java-Strings
Blogathon
Java
Java-Strings
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Import JSON Data into SQL Server?
How to Create a Table With Multiple Foreign Keys in SQL?
How to Install Tkinter in Windows?
SQL Query to Convert Datetime to Date
SQL Query to Create Table With a Primary Key
Arrays in Java
Split() String method in Java with examples
For-each loop in Java
Arrays.sort() in Java with examples
Reverse a string in Java