Java lastIndexOf Method

The Java lastIndexOf Method is one of the String Methods, which is to return the index position of the last occurrence of a specified string. If the specified text is not found, this method will return -1.

In this article, we will show how to use String lastIndexOf in Java Programming language with an example. The syntax of the string lastIndexOf is as shown below.

Java lastIndexOf Method syntax

The Java Programming Language provides four different string lastIndexOf methods to return the index position of the last occurrence.

The first Java lastindexof method will accept the character as the parameter and return the index position of the last occurrence of the specified character.

This second method will accept the character and the last index position (End_index) as the second argument. This method will look from index position 0 to the End_index index position. Next, it returns the last occurrence of the specified character.

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

public int lastIndexOf(int ch, int End_Index);
String_Object.lastIndexOf(int ch, int End_Index)

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

The second method will accept the string data and End_index. The Java lastIndexOf method will start looking from index position 0 to the End_index index position and return the specified substring’s last occurrence.

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

public int lastIndexOf(String str, int End_Index);
String_Object.lastIndexOf(String str, int Starting_Index)
  • String_Object: Please specify the valid Object.
  • End_Index: Please specify the ending index position. If you specify this value, this function will start looking from index position 0 to this position (not to the end).

NOTE: If you specify the negative value as the End_Index, the Java function will return -1.

Java lastIndexOf Method Example

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

package StringFunctions;

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

This Java example calls the public int lastIndexOf (int ch) to find the index position of the last occurred ‘i’.

int a = str.lastIndexOf('i');

The following Java string statement calls the public int lastIndexOf (int ch, int End_Index) method to find the index position of ‘i’ starting from index position 0 to 10. As we all know, i is located only once before the index position 10. So, it has returned the same.

int b = str.lastIndexOf('i', 10);

Next, we used the negative value as the End_Index.

int c = str.lastIndexOf('i', -10);

In the next line, we look for the non-existing item ‘y’ inside the str. It will call the public int lastIndexOf (int ch) to find the last index position of ‘y’. Since it does not find, it is returning -1 as output.

int d = str.lastIndexOf('y');

It calls the Java public int lastIndexOf (String str) method to find the index position of a substring ‘abc’. And then, it stores the last index position value in variable e.

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

NOTE: You should count the space as One Character in this String Method.

From the above statement, although the term abc is repeated multiple times, the Java string lastIndexof function returns the index position of the last occurrence. Now, let us provide the end index position.

The following Java statement will call a public int lastIndexOf(String str, int End_Index) method. And it returns the last occurrence of a string abc starting at index position 0 to 21.

int f = str1.lastIndexOf("abc", 21);

Let us change the End_Index value to 29. It means the method returns the last occurrence of abc starting at 0 to 29.

int g = str1.lastIndexOf("abc", 29);

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