Strings are the most important aspect of programming in Java. To deal with strings/text, Java offers a built-in String class that consists of numerous methods. These methods allow users to access and manipulate textual data effectively and conveniently. One of these methods is the indexOf() method which assists us in finding the position or the index of the character or string.
In this article, we will illustrate the implementation of Java’s indexOf() method using different use cases.
How to Use String.indexOf() in Java?
The indexOf() method of string in Java returns the position or the index of the declared character. The string is declared and the specified character is passed from the string to the indexOf() method, this method moves through characters in the string from start to end and returns the index of the specified character.
There are four different variants for the indexOf() Method in Java that are described below.
- indexOf()
- int indexOf(String str)
- int indexOf(char c, int i)
- int indexOf(String str, int i)
Let us see the details of each variant through code examples in detail.
Variant 1: indexOf()
This variant retrieves the index of a particular character from the string. It retrieves a value of “-1” if the character is not present/found in the string. The code below shows how the indexOf() method works when we pass the character “J”.
public class Variant1 {
public static void main(String args[])
{
//Declare the String
String s = new String("Learn Java and Python through Code");
System.out.print("Found the element at Position number : ");
//Print the position of the element
System.out.println(s.indexOf('J'));
}
}
Output
The output below shows that the first occurrence of “J” is at position “6”.

Variant 2: indexOf(String str) Method
The indexOf(String str) returns the starting position of the specified substring from the string. The code below implements this method to get the initial position/index of the string.
public class Variant3 {
public static void main(String args[])
{
//Declare the String
String s = new String("Learn Java and Python through Code");
String x = new String("Java");
System.out.print("Found the element at Position Number: ");
//Print the position of the element
System.out.println(s.indexOf(x));
}
}
In the above Java code
- A public class is declared as “Variant3” in Java.
- Next, a string is declared as “s”.
- The next step involves the declaration of a substring “Java” to find the index.
- The indexOf() method calculates the index of the substring and returns it as output.
Output
The below output indicates that the substring “Java” is present at position 6.

Variant 3: int indexOf(char c, int i)
This method returns the index of the character after the declared position. The code below returns the next occurrence of character “a” after position “5”.
public class Variant2 {
public static void main(String args[])
{
//Declare the String
String s = new String("Learn Java and Python through Code");
System.out.print("Found the element after index number 5 at Position number : ");
//Print the position of the element
System.out.println(s.indexOf('a', 5));
}
}
Output
The output below shows that the character “a” is appearing at index “7” after its first occurrence at index “5”.

Variant 4: indexOf(String str, int i) Method
This method returns the index of a substring appearing after the specified index. In the code below, the position of substring “Java” is required after index “8”.
public class Variant4 {
public static void main(String args[])
{
//Declare the String
String s = new String("Learn Java and teach Java");
String x = new String("Java");
System.out.print("Found the element 'Java' after 8th index at Position Number: ");
//Print the position of the element
System.out.println(s.indexOf(x,8));
}
}
Output
In the below output, the element or substring “Java” appears at position number “21” after the eighth index.

The next section will elaborate on certain applications where the indexOf() method is used.
Applications of IndexOf() Method in Java
The indexOf() method in Java is one of the useful methods of String in Java. The method is utilized in a wide variety of applications and some of these are listed below.
- To count specified characters in a String
- To check the availability of a character in the string
- To distinguish between an alphabet, digit, and special character
- To check vowels or consonants
Let us implement these applications using the code examples.
Application 1: To Count Specified Characters in a String
The indexOf() method is used to count the total occurrences of specified characters in a string which is depicted in the code below.
import java.io.*;
//The class in Java
class Application1 {
public static void main (String[] args) {
//Declare the string
String st1="Java is a programming language";
int total=0;
//The for loop to iterate over the string
for(int x=0;x<st1.length();x++)
{
//The number of times "a" occurs
x=st1.indexOf('a',x);
if(x<0)
break;
total++;
}
//Output
System.out.println("The total count of 'a' in the string is: "+ total);
}
}
In the above code
- The class is declared as “Application1”.
- A string in Java is declared/created as “st1”.
- In the next step, the total or the count is initialized.
- The for loop loops through the characters in the declared string.
- The next step involves the declaration of the indexOf() method to calculate the total occurrence of the character “a”.
Output
The output below depicts that the character “a” appears for a total of “6” times in the entire string.

Application 2: To Check for the Availability of a Character in the String
The code below uses the indexOf() method to check for the availability of a character along with the position.
class Application2 {
public static void main(String args[])
{
//Declare the String
String st = new String("Java is a programming language");
String x1 = new String("c");
System.out.print("Found the element at Position Number: ");
//Print the position of the element
System.out.println(st.indexOf(x1));
}
}
In the above code, the character “c” declared as “x1” is searched in the string “st”.
Output
The below output shows that the character “c” is present at position “-1” since the indexOf() method by default returns this value for the unavailability of the character.

Application 3: To Check for the Availability of Digits, Alphabet, and Special Characters in the String
The indexOf() method is also useful to check for digits, alphabets, and special characters as shown in the code below.
class Application3
{
public static void test(char a)
{
//Check for the digits
if("12345".indexOf(a)>=0) {
System.out.print("This is digit\n");
}
//Check for the alphabets
else if("[ABCDEFGH]".indexOf(Character.toUpperCase(a))>=0)
{
System.out.print("This is alphabet\n");
}
//If it is neither a digit nor an alphabet, it will be a special character
else{
System.out.print("This is Special character\n");
}
}
// Test the code
public static void main(String[] args)
{
test('h');
test('4');
test('%');
}
}
In the above code block
- A class is declared as “Application 3”.
- The main method is declared as a “test” that checks for the digits, alphabets, and special characters.
- The “if” statement checks for the availability of digits in the string.
- The else-if statement checks for the availability of alphabets in the string.
- The last statement “else” checks the special character.
- The main method of Java tests for the digits alphabets and special characters.
Output
The output below depicts that the entered inputs are the alphabet, digit, and special character respectively.

Application 4: Checking for Vowels
The indexOf() method in Java allows one to check for vowels or consonants. The code below demonstrates the implementation of the indexOf() method for checking the vowels or consonants.
class Application4
{
public static boolean testvowel(char a)
{
//Enter the vowels
return "aeiou".indexOf(Character.toLowerCase(a))>=0;
}
//Main method of Java
public static void main(String[] args)
{
boolean check = testvowel('e');
if(check)
System.out.println("It is Vowel");
else
System.out.println("It is Consonant");
}
}
In the code above, the only difference that occurs from the previous code is that the vowels are added in the return statement and all other alphabets are considered consonants. The main method of Java checks for the vowels and prints the result accordingly.
Output
In the below output the alphabet “e” is printed as a vowel using the indexOf() method of Java string.

This concludes the discussion on different variants and applications of the indexOf() method.
Conclusion
The Java’s string.indexOf() method retrieves the index or the position of the character or a substring. There are four variants of this method and multiple applications like finding the total count of a certain character in the string, checking if the character is present in the string or not, etc. In this article, we have discussed the implementation of the indexOf() method using its variants and different applications.