The Java indexOf() method finds the index position at which a specified string begins. This method lets you find a string within another string. If the value you specify is not present in the string through which you are searching, indexOf() returns -1.
There are scenarios where you may want to retrieve the index of a particular character value or substring from within a larger string.
For instance, suppose you are writing an inventory program for a fruit stand. You have a list of fruits in a string. One of your tasks may be to find where Mango appears in a list of fruits formatted as a string. This will let you test your program.
That’s where the indexOf() Java method comes in. indexOf() returns the index of a particular character or substring in a string. This tutorial will explore how to use the indexOf() method in Java, with reference to examples.
Java indexOf() Method
The Java string indexOf() method retrieves the index value associated with a particular character or substring in a string. If the character or phrase does not occur in the string, indexOf() returns -1.
The syntax of the indexOf() method is as follows:
string_name.indexOf(substring, start_char)
The indexOf() method takes in two parameters:
- substring is the character or substring whose start index position you want to find (required)
- start_char is the index position at which the search for the substring should begin (optional)
Let’s walk through an example to illustrate the indexOf() method in Java and how it finds an occurrence of a specified character.
indexOf() Java Example
Suppose we are operating a fruit stand, and we have a string that contains the top-five most popular fruits sold last month. We know that Strawberry was the third-most-popular fruit, but we want to know at what position it is stored in our list of fruits.
To accomplish this task, we could use the indexOf() method. Here’s an example that searches for the Java string Strawberry in our list of fruits. This example returns the index within this string at which the string begins:
public class FindStrawberry { public static void main(String args[]) { String fruits = "Apple Pear Strawberry Grape Mango"; int strawberry = fruits.indexOf("Strawberry"); System.out.println("Strawberry appears at the index position " + strawberry + "."); } }
Our code returns:
Strawberry appears at the index position 11.
We define a class called FindStrawberry which stores the code for our procedure. Then, we define a Java variable called fruits. This variable stores the list of the top-five most popular fruits stored last month.
Next, we use the indexOf() method to find where the substring Strawberry starts in the fruits string. Finally, we print out a message to the console, which states, “Strawberry appears at the index position “. This statement is followed by the index number calculated by the indexOf() method and stored in the strawberry variable.
indexOf() Java Example: Start at a Specified Index
We can use the optional start_char method to instruct indexOf() to start searching for a substring after a certain index value in our string.
For instance, suppose that we want to find out the index position at which Mango appears in our string. We know that Mango is less popular than Strawberry, and we also know that it appears in the top-five.
Strawberry starts at the index position 11, as we discovered above, and ends at the index position 20 (11 + length of the word Strawberry). So, we know that our search should start at index position 20.
We could use the indexOf() method to find the index value at which Mango begins in our fruits string. Here’s the code we would use:
public class FindMango { public static void main(String args[]) { String fruits = "Apple Pear Strawberry Grape Mango"; int mango = fruits.indexOf("Mango", 20); System.out.println("Mango appears at the index position " + mango + "."); } }
Our code returns:
Mango appears at index position 28.
This example is similar to our first one. But, in this example we have substituted our variable and class names to use the word mango instead of strawberry.
We have specified the start_pos parameter in the indexOf() method and assigned it the value 20. This means indexOf() will start searching for Mango after the index position 20 in our fruits string.
Handling a String That is Not Present
You may be wondering what you should do if the value for which you are searching does not appear in your target string.
If your program depends on a value being present in a string, you should check if the value exists before using indexOf(). You can check if a value exists using a Java if statement:
public class FindMango { public static void main(String args[]) { String fruits = "Apple Pear Strawberry Grape Mango"; int mango = fruits.indexOf("Mango", 20); if (mango != -1) { System.out.println("Mango appears at the index position " + mango + "."); } else { System.out.println("Mango is not in the specified string."); } } }
We have added an if statement in which our print statements are enclosed.
If our indexOf() method returns -1, our program could not find a value in the specified string. Our if statement checks if “mango” is equal to -1. If it is, the contents of the “else” statement execute. If “mango” is not equal to -1, our “if” statement executes.
"Career Karma entered my life when I needed it most and quickly helped me match with a bootcamp. Two months after graduating, I found my dream job that aligned with my values and goals in life!"
Venus, Software Engineer at Rockbot
Conclusion
The indexOf() method is used in Java to retrieve the index position at which a particular character or substring appears in another string. You can use a second argument to start your search after a particular index number in the string.
If the specified letter or letters cannot be found, indexOf() returns -1.
Do you want to become a professional Java developer? Check out our How to Learn Java guide for expert tips on learning this industry-standard programming language.
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.