Java String indexOf() Method with Example | Definition, Syntax, Usage & Types of indexOf() String Method in Java

In this, we will learn the string indexOf() method with an example. The string class provides four variants of the indexOf() method. Here, we will discuss all the four variants of the Java String indexOf() Method. Let’s discuss it one by one.

Java String indexOf() – Definition & Syntax

TheindexOf()method in Java returns the index of the first occurrence of specified character(s) in a string. There are 4 variations of this method in String class as mentioned in the below image.

Java String indexOf() Method with Example 1

Syntax

The syntax of the String indexOf() Method in Java is:

public int indexOf(int cha)

Java indexOf() Parameters

Following are the indexOf() Java String parameters:

  • char – Represent a single character value
  • str – Represent the string to search for
  • fromIndex – Represent the index position to start the search from

Return Value of indexOf() Method in Java

  • returns the index of the first occurrence of the specified character/string
  • returns -1 if the defined character/string is not found

Also Read:

Java String indexOf() Method Example

public class IndexOfExample{
public static void main(String args[]) {
String str1 = new String("This is a Btechgeeks tutorial");
String str2 = new String("Beginners");
String str3 = new String("Book");
String str4 = new String("Books");
System.out.println("Index of B in str1: "+str1.indexOf('B'));
System.out.println("Index of B in str1 after 15th char:"+str1.indexOf('B', 15));
System.out.println("Index of B in str1 after 30th char:"+str1.indexOf('B', 30));
System.out.println("Index of string str2 in str1:"+str1.indexOf(str2));
System.out.println("Index of str2 after 15th char"+str1.indexOf(str2, 15));
System.out.println("Index of string str3:"+str1.indexOf(str3));
System.out.println("Index of string str4"+str1.indexOf(str4));
System.out.println("Index of hardcoded string:"+str1.indexOf("is"));
System.out.println("Index of hardcoded string after 4th char:"+str1.indexOf("is", 4));
}
}

Output:

Index of B in str1: 10
Index of B in str1 after 15th char:19
Index of B in str1 after 30th char:-1
Index of string str2 in str1:10
Index of str2 after 15th char-1
Index of string str3:19
Index of string str4-1
Index of hardcoded string:2
Index of hardcoded string after 4th char:5

The indexOf() Method Signature

There are four types of indexOf() method in java. From the following table, you will learn the signature of indexOf methods in Java:

No. Method Description
1 int indexOf(int ch) returns index position for the given char value
2 int indexOf(int ch, int fromIndex) returns index position for the given char value and from the index
3 int indexOf(String substring) returns index position for the given substring
4 int indexOf(String substring, int fromIndex) returns index position for the given substring and from the index

For detailed information about these four types of java string indexOf() method, just take a look at the below sections:

1. indexOf(int ch)

This is the first variant of the Java String indexOf() method. This method returns the index of the first occurrence of the specified character. If the specified character is not present there then it will return -1.

Syntax:

The syntax of the first variant of the indexOf() method is given below:

public int indexOf(int ch)

Here, ch is a character whose index we want to return.

Java String indexOf(int ch) Method Example

class StringIndexOf{
public static void main(String args[]){
String str = "String tutorial by btechgeeks";
//returns index of first
//occurrence of a.
System.out.println(str.indexOf('a')); //13
}
}

Output:

13

2. indexOf(int ch, int fromIndex)

This is the second variant of the Java String indexOf() method. This method returns the index of the first occurrence of the specified character, starting the search from the specified index. If the specified character is not present there then it will return -1.

Syntax:

The syntax of the second variant of the indexOf() method is given below:

public int indexOf(int ch, int fromIndex)

Here, ch is a character and fromIndex is the index position at which point the search begins.

Java String indexOf(int ch, int fromIndex) Method Example

class StringIndexOf{
public static void main(String args[]){
String str = "String tutorial in java by btechgeeks";
//returns index of a
//after 15th index at position.
System.out.println(str.indexOf('a', 15)); //21
}
}

Output:

21

3. indexOf(String str)

This is the third variant of the Java String indexOf() method. This method returns the index of the first occurrence of the specified substring. If the specified substring is not present there then it will return -1.

Syntax:

The syntax of the third variant of the Java String indexOf() method is given below:

public String indexOf(String str)

Here, str is a substring whose index we want to return.

Java String indexOf(String str) Method Example

class StringIndexOf{
public static void main(String args[]){
String str = "String tutorial in java by btechgeeks";
//returns the index of
//initial character of substring
System.out.println(str.indexOf("java")); //20
}
}

Output:

20

4. indexOf(String str, int fromIndex)

This is the fourth variant of the indexOf() method. This method returns the index of the first occurrence of the specified substring, starting the search from the specified index. If the specified substring is not present there then it will return -1.

Syntax:

The syntax of the fourth variant of the indexOf() method is given below:

public String indexOf(String str, int fromIndex)

Here, str is a substring and fromIndex is the index position at which point the search begins.

Java String indexOf(String str, int fromIndex) Method Example

class StringIndexOf{
public static void main(String args[]){
String str = "String tutorial in java by btechgeeks";
//returns the index of initial character
//of substring after 20th position
System.out.println(str.indexOf("java", 20)); //27
//returns -1 because specified substring
//is not present there.
System.out.println(str.indexOf("Java Tutorial", 10)); //-1
}
}

Output:

27
-1

Leave a Comment