All About indexOf() Method of String Class in Java

All About indexOf() Method of String Class in Java

3 mins read432 Views Comment
Updated on May 11, 2023 10:14 IST

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.

2023_05_Feature-Image-Templates-25.jpg

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);
}
}
Copy code

Output

2023_05_public-int-indexOfchar-c.jpg

Must Check: Top Free Online Java Courses and Certifications

Must Check: Top Online Java Courses and Certifications

Recommended online courses

Best-suited Java courses for you

Learn Java with these high-rated online courses

โ€“ / โ€“
350 hours
Free
6 months
โ€“ / โ€“
4 months
โ€“ / โ€“
โ€“ / โ€“
โ€“ / โ€“
1 month
โ‚น40 K
7 weeks
โ‚น7.8 K
3 months
โ‚น8.47 K
2 months
โ‚น7 K
6 months
โ‚น4 K
2 months

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);
}
}
Copy code
2023_05_public-int-indexOfint-char-int-fromIndex.jpg

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);
}
}
Copy code

Output:

2023_05_public-int-indexOfString-s.jpg

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);
}
}
Copy code

Output

2023_05_public-int-indexOfString-s-int-fromIndex.jpg

Also Read: What is Java?

Application of indexOf() Method of String Class

  1. To know whether a character is a vowel or constant.
  2. Check Whether the Input is a Digit, Letter, or Special Character
  3. 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");
}
}
}
Copy code

Output:

2023_05_To-know-whether-a-character-is-vowel-or-constant.jpg

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);
}
}
Copy code

Output:

2023_05_To-Check-Whether-the-Input-is-a-Digit-or-Letter-or-Special-Character.jpg

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);
}
}
Copy code

Output

2023_05_To-count-the-occurrence-of-a-specified-character-in-the-String.jpg

Contributed By: Shubham Kumar

About the Author

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