DEV Community

Cover image for Java indexOf() Method
Ritvik Dubey
Ritvik Dubey

Posted on

Java indexOf() Method

Hello all๐Ÿ‘‹ I hope you are doing well. Again this is going to be a very short and very useful article. In this article I'll be writing about indexOf() method in Java. This is a one of the most useful methods.

Let's begin...

indexOf()

This method lets you find a string within another string. The indexOf() method searches for the first occurrence of a character or substring. This method returns the index position of the first occurrence of a specified string. This is a method of Java String class. In other words, this 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.

Syntax:-

stringName.indexOf(char ch);
Enter fullscreen mode Exit fullscreen mode

There are different variations in which indexOf() can be used:-

1. indexOf(char ch)

In this variation we have printed the index value of first occurrence of character i

public class Demo {
    public static void main(String[] args) {
        String str = "We will find index of i in this";
        System.out.println("Index of the character i is : " + str.indexOf('i'));
    }
}
Enter fullscreen mode Exit fullscreen mode

You can run your code online here

java-indexOf().png

2. indexOf(char ch, int start)

In this variation we have printed the index value of character i but not at its first occurrence, the character i first occurs at index 4, so we gave it starting value greater than 4

public class Demo {
    public static void main(String[] args) {
        String str = "We will find index of i in this";
        System.out.println("Index of the character i is : " + str.indexOf('i', 5));
    }
}
Enter fullscreen mode Exit fullscreen mode

You can run your code online here

java-indexOf(char ch, int start).png

3. indexOf(String str)

In this variation we have printed the index value of first occurrence of string in

public class Demo {
    public static void main(String[] args) {
        String str = "We will find index of in here";
        System.out.println("Index of the String in is : " + str.indexOf("in"));
    }
}
Enter fullscreen mode Exit fullscreen mode

You can run your code online here

java-indexOf(String str).png

4. indexOf(String str, int start)

In this variation we have printed the index value of String in but not at its first occurrence, the String in first occurs at index 9, so we gave it starting value greater than 9

public class Demo {
    public static void main(String[] args) {
        String str = "We will find index of in here";
        System.out.println("Index of the String in is : " + str.indexOf("in", 10));
    }
}
Enter fullscreen mode Exit fullscreen mode

You can run your code online here

java-indexOf(String str, int start).png

A common scenario can be when a system admin wants to find the index of the โ€˜@โ€™ character of the email Id of a client and then wants to get the remaining substring. In that situation, indexOf method can be used.

Okay so that's enough for now.

Thank you for reading.

Please share your thoughts about it and correct me if I'm wrong.

I hope you liked it and found it helpful.

Cover:- Rajat Gour

Connect with me on Twitter or LinkedIn

My personal blog blog.ritvikdubey.com

Discussion (1)

Collapse
aartiyadav profile image
Aarti Yadav

Great Explanation about Java indexOf() Method but I have one question why you are prefering run java program on one source. If you search on google their are lots of better online java compiler for example this online compiler I personally run the java program there and would like to suggest you should also try it.