Java lastIndexOf Method

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

In this article, we will show how to use String lastIndexOf in Java Programming language with example. The syntax of the string lastIndexOf in Java Programming language 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 following Java lastindexof method will accept the character as the parameter and return the index position of the last occurrence of the specified character.

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

//In order to use in program
String_Object.lastIndexOf(int ch)

This following method will accept the character as the first parameter and last index position (End_index) as the second argument. Java String lastIndexOf method will start looking 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, int End_Index); // It will return Integer 

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

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

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

The below lastIndexOf method will accept the string data as the first parameter and last index position (End_index) as the second argument.

Java String.lastIndexOf method will start looking from index position 0 to the End_index index position and returns the last occurrence of the specified substring.

public int lastIndexOf(String str, int End_Index); // It will return Integer 

//In order to use in program
String_Object.lastIndexOf(String str, int Starting_Index)
  • String_Object: Please specify the valid String Object.
  • End_Index: Please specify the ending index position. If you specify this value, the string.lastIndexOf 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 String.lastIndexOf function will return -1.

Java lastIndexOf Method Example

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

//Java String lastIndexOf example
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

Within this Java lastindexof, it calls the public int lastIndexOf (int ch) to find the index position of last occurred ‘i’

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

The following Java string lastIndexOf 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 index position 10. As we all know, i is located only once before the index position 10. So, the lastindexof function 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 are looking for 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 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 the lastIndexOf String Method.

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

The following 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 lastIndexOf method returns the last occurrence of string abc starting at index position 0 to 29.

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

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