Java indexOf Method

The Java indexOf Method is one of the String Methods that return the index position of the first occurrence of a specified string. If the specified string is not found, the Java indexOf method will return -1.

This article will show how to use String indexOf in Java Programming language with an example. Before we get into the example, the basic syntax of the string.indexOf is shown below.

Java indexOf Method syntax

This Programming Language provides four different methods to return the index position. The following first method of the Java indexOf will accept the character as the parameter and return the index position of the first occurrence of the specified character.

The second one accepts the character and Starting index position (Starting_index), which will start looking from the Starting_index.

public int indexOf(int ch); // It will return Integer 
String_Object.indexOf(); //In order to use in program

public int indexOf(int ch, int Starting_Index); 
String_Object.indexOf(int ch, int Starting_Index)

The following Java indexof method will accept the string data as the argument and return the index position of the first occurrence of the specified substring.

The second method will accept the string data and Starting_index as the second argument. The String indexOf method in Java will start looking from the Starting_index. And it returns the index position of the first occurrence of the specified substring.

public int indexOf(String str); // It will return Integer 
String_Object.indexOf(String str)

public int indexOf(String str, int Starting_Index);
String_Object.indexOf(String str, int Starting_Index)
  • String_Object: Please specify the valid String Object.
  • Starting_Index: Please specify the starting index position. If you specify this value, it will start looking from index position 0 instead of looking from it.

NOTE: If you specify the negative value as the Starting_Index, it will start looking from index 0.

Java indexOf Method Example

The indexOf method returns the index position of the first occurrence of a specified string. This Java program will help to understand the indexOf method.

package StringFunctions;

public class IndexOfMethod {
	public static void main(String[] args) {
		String str = "Tutorials On Java Programming";
		String str1 = "We are abc working at abc company";
		
		int a = str.indexOf('a');
		int b = str.indexOf('a', 10);
		int c = str.indexOf('a', -10);
		int d = str.indexOf('z');
		
		int e = str1.indexOf("abc");
		int f = str1.indexOf("abc", 9);
		int g = str1.indexOf("abc", 25);
		int h = str1.indexOf("abc", -25);
		
		System.out.println( "Index position of a = " + a);
		System.out.println( "Starting from 10 - Index position of a = " + b);
		System.out.println( "Start from -Ve Index - Index position of a = " + c);
		System.out.println( "Index position of z = " + d);
		System.out.println( "Index position of String abc = " + e);
		System.out.println( "Starting from 10 - Index position of String abc = " + f);
		System.out.println( "Starting from 25 - Index position of String abc = " + g);
		System.out.println( "Start from -Ve Index - Index position of String abc = " + h);
	}
}
Java indexOf Method 1

Within this Java indexOf Method example, we declared two String variables, Str1 and Str2. Next, we assigned corresponding values using the first two statements.

For a, it will call the indexOf(int ch) method to find the index position of ‘a’

The following b statement will call the indexOf (int ch, int Starting_Index) method to find the index position of ‘a’ starting at 10.

Next, we used the negative value as the Starting_Index. The Java function will start looking for a’ starting at 0.

In the next line of this Java program d, we are looking for the non-existing item ‘z’ inside the str. It will try to find the index position of ‘z’. Since it does not find, it is returning -1 as output.

This Java indexof statement calls the indexOf(String str) method to find the position of a substring ‘abc’ and store the index value in variable e. Remember, you should count the space as One Character. Although the term abc is repeated multiple times, the Indexof function returns the index of a first occurrence.

For f, let us provide the starting value. It will call the Java indexOf (String str, int Starting_Index) to return the first occurrence of abc starting at 9.

In the next line, we specified the starting point as 25 for g. And we all know that there is no substring “abc” after the index position 25. So, the String Method is returning -1.

Lastly, we used System.out.println statements to print the output.