All About indexOf() Method of String Class in Java
Explore the power of the indexOf() method in Javaโs String class with our in-depth guide. Understand its syntax, return values, overloads, and practical uses in string manipulation. Suitable for newbies as well as experienced Java developers looking to refine their understanding of core Java functionality.
This article will discuss the indexOf() method of the String class in Java. We understand the concept of this method using example programs, and last, we will discuss the application of the indexOf method using coding problems.
Must Read: Strings in Java
The signature of the indexOf() method is defined as follows.
public int indexOf(char c)
The indexOf(int char) method takes a character value and returns the index of the first occurrence of that character in the string. In the below program, we initialized a String str with the value and passed the character โcโ as an argument to the indexOf() method. The method returns the character โcโ index from the String str. At last, print the index of character โcโ in the output.
Code:
public class Main { public static void main(String[] args) { String str="I love to code"; int index= str.indexOf('c'); System.out.println("c is at index="+index); }}
Output
Must Check: Top Free Online Java Courses and Certifications
Must Check: Top Online Java Courses and Certifications
Best-suited Java courses for you
Learn Java with these high-rated online courses
public int indexOf(int char, int fromIndex)
The indexOf(int char, int fromIndex) method takes two arguments one is a character value, and the other one is the index of the string from which the index of that character is calculated. It returns the index of that particular character from the start of the index we provided in the second argument. In the below program, String str is initialized with the value, and we pass characters โoโ and index four, from which the index of โoโ is calculated. At last, print the output of index โoโ.
Code:
public class Main { public static void main(String[] args) { String str="I love to code"; int index= str.indexOf('o', 4); System.out.println("o is at index="+index); }}
public int indexOf(String s)
The indexOf(String s) method takes one argument string and returns the index of the first occurrence of the substring in the string. In the below program, we initialized a string and passed the substring โhaveโ in the method. This indexOf method returns the โhaveโ substring index in the String str. Observe the output.
Code:
public class Main { public static void main(String[] args) { String str="Code is like humor. When you have to explain it, itโs bad"; int index= str.indexOf("have"); System.out.println("have is at index="+index); }}
Output:
public int indexOf(String s , int fromIndex)
The method indexOf(String s, int fromIndex) takes two arguments: string and index of type integer. This method returns the substring index from the specified index as we have passed as the second argument. In the below program, we have initialized String str and passed substring โtoโ and index ten as arguments in the method. The method returns the substring index โtoโ in the output.
Code:
public class Main { public static void main(String[] args) { String str="Java is to Javascript what car is to Carpet"; int index= str.indexOf("to",10); System.out.println("to is at index="+index); }
}
Output
Also Read: What is Java?
Application of indexOf() Method of String Class
- To know whether a character is a vowel or constant.
- Check Whether the Input is a Digit, Letter, or Special Character
- To Count the Occurrence of a Specified Character in the String
To Know Whether a Character is Vowel or Constant
In the below program, the user enters the character and checks the method return true if it is a vowel or else false if it is constant.
Code:
import java.util.Scanner;
public class Main { //method checking a character is vowel or constant public static boolean check(char ch) { return "aeiou".indexOf(Character.toLowerCase(ch)) >=0; } public static void main(String[] args) { //Scanner class for taking user input Scanner sc =new Scanner(System.in); System.out.println("Enter a character"); // Taking character input char ch=sc.next().charAt(0); if(check(ch)) { System.out.println(ch +" is a vowel"); }else { System.out.println(ch+" is a constant"); } }}
Output:
To Check Whether the Input is a Digit or Letter or Special Character
In the below program, we will check whether enter input is present in a predefined set of strings and return the result as per condition satisfied as character or digit or special character.
Code:
import java.util.Scanner;
public class Main { //method checking input is a character or digit or symbol public static void check(char ch) { if("0123456789".indexOf(ch)>=0) { System.out.println(ch+" is a digit"); }else if("[abcdefghijklmnopqrstuvwxyz]".indexOf(ch)>=0) { System.out.println(ch+" is a character"); }else { System.out.println(ch + " is a symbol"); } } public static void main(String[] args) { //Scanner class for taking user input Scanner sc =new Scanner(System.in); System.out.println("Enter a character or a digit or any symbol"); // Taking input char ch=sc.next().charAt(0); check(ch); }}
Output:
To Count the Occurrence of a Specified Character in the String
In the below program, we check whether the character is present in the string, and if it is present, then we increment the count variable. Do the same thing until the index is not found.
Code
public class Main { public static void main(String[] args) { String str="Shiksha Online"; int count=0; for(int i=0;i<str.length();i++) { i=str.indexOf('a',i); if(i<0) break; count++; } System.out.println("Count of a is "+ count); }
}
Output
Contributed By: Shubham Kumar
This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio