Wednesday, July 3, 2024
HomeLanguagesJavaApplications of String indexOf() Method in Java

Applications of String indexOf() Method in Java

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: 

  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 neveropen";
  
        // 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

Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments