Java indexOf Method

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

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

Java indexOf Method syntax

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

public int indexOf(int ch); // It will return Integer 

//In order to use in program
String_Object.indexOf()

It will accept the character as the first parameter and Starting index position (Starting_index) as the second argument. Java String.indexOf method will start looking from the Starting_index and return the index position of the first occurrence of the specified character.

public int indexOf(int ch, int Starting_Index); // It will return Integer 

//In order to use in program
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.

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

//In order to use in program
String_Object.indexOf(String str)

It will accept the string data as the first parameter and Starting index position (Starting_index) as the second argument. Java String.indexOf method 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, int Starting_Index); // It will return Integer 

//In order to use in program
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 then, instead of looking from index position 0 String.indexOf function will start looking from this position.

NOTE: If you specify the negative value as the Starting_Index then, Java String.indexOf function will start looking from index 0.

Java indexOf Method Example

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

//Java String indexOf example
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 string indexOf Method example, we declared two String variables Str1, Str2. Next, we assigned corresponding values using the following statement.

String str = "Tutorials On Java Programming";
String str1 = "We are abc working at abc company";

It will call the public int indexOf (int ch) method to find the index position of ‘a’

int a = str.indexOf('a');

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

int b = str.indexOf('a', 10);

Next we used the negative value as the Starting_Index. Java string Indexof function will start looking for a’ starting at index position 0.

int c = str.indexOf('a', -10);

In the next line of this Java program, we are looking for the non-existing item ‘z’ inside the str. It will call the public int indexOf (int ch) method to find the index position of ‘z’. Since it does not find, it is returning -1 as output.

int d = str.indexOf('z');

This Java indexof statement calls the public int indexOf (String str) method to find the index position of a substring ‘abc’ and store the index value in variable e.

int e = str1.indexOf("abc");

NOTE: You should count the space as One Character.

From the above statement, although term abc repeated multiple times, the Java string Indexof function is returning the index of a first occurrence.

Now, let us provide the starting index position for the indexof. It will call the public int indexOf (String str, int Starting_Index) to return the first occurrence of string abc starting at index position 9.

int f = str1.indexOf("abc", 9);

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

int g = str1.indexOf("abc", 25);

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