Java indexOf() Method
Tags: java, beginners, computerscience, javascript
Published: 2021-09-05 12:06:59

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:-**
```Java
stringName.indexOf(char ch);
``` 
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**   

```Java
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'));
    }
}
``` 
***You can run your code online [here](https://www.tutorialspoint.com/compile_java_online.php)***

![java-indexOf().png](https://cdn.hashnode.com/res/hashnode/image/upload/v1630834863650/aHJoPy0_6.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

```Java
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));
    }
}
``` 
***You can run your code online [here](https://www.tutorialspoint.com/compile_java_online.php)***

![java-indexOf(char ch, int start).png](https://cdn.hashnode.com/res/hashnode/image/upload/v1630835735091/oupy6dR_a.png)

### 3. indexOf(String str)
In this variation we have printed the index value of first occurrence of string **in**   

```Java
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"));
    }
}
``` 
***You can run your code online [here](https://www.tutorialspoint.com/compile_java_online.php)***

![java-indexOf(String str).png](https://cdn.hashnode.com/res/hashnode/image/upload/v1630835458584/fzvxKDDqx.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

```Java
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));
    }
}
``` 
***You can run your code online [here](https://www.tutorialspoint.com/compile_java_online.php)***

![java-indexOf(String str, int start).png](https://cdn.hashnode.com/res/hashnode/image/upload/v1630836382449/qBfrvl17Y.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](https://twitter.com/rajatgour20)***
#### *Connect with me on [Twitter](https://twitter.com/ritvikdubey27?s=09) or [LinkedIn](https://www.linkedin.com/in/ritvik-dubey-3bbb22169)*
#### *My personal blog [blog.ritvikdubey.com](https://blog.ritvikdubey.com/)*