In this tutorial, you will learn about String lastIndexOf() method, its syntax, and usage with examples.

Java String lastIndexOf() method

In Java, String lastIndexOf() method is used to find the index of last occurrence of given search value in the string.

lastIndexOf() method searches the given search value in the string from end of the string to the beginning of the string.

Syntax of lastIndexOf()

There are four lastIndexOf() methods defined in String class. Based on the type of argument, and number of arguments passed, respective method is called.

</>
Copy
public int lastIndexOf(String str)
public int lastIndexOf(String str, int fromIndex)
public int lastIndexOf(int char)
public int lastIndexOf(int char, int fromIndex)

Let us summarise all these four definitions, and specify a single syntax to call String lastIndexOf() method.

</>
Copy
string.lastIndexOf(value, fromIndex)

where

ParameterDescription
value[Mandatory]
A String or an int type value. (If int is given, then the integer represents a character value like ‘a’, ‘b’, ‘A’, ‘5’, etc.)
The value which is searched in the string to find the index of.
fromIndex[Optional]
An int type value.
The (index) position in the string from where search for the value happens. And the search happens from this index towards the beginning of the string.

lastIndexOf() returns value of int type.

If the given value is present in the string, then lastIndexOf() method returns the last index of the value in the string.

If the given value is not present, then lastIndexOf() method returns -1.

The following are some of the examples to call lastIndexOf() on str string with valid arguments.

</>
Copy
// lastIndexOf(String str)
str.lastIndexOf("World")

// lastIndexOf(String str, int fromIndex)
str.lastIndexOf("World", 20)

// lastIndexOf(int char)
str.lastIndexOf(65)
str.lastIndexOf('A')

// lastIndexOf(int char, int fromIndex)
str.lastIndexOf(65, 4)
str.lastIndexOf('A', 4)

Examples

1. lastIndexOf() – Find the last index of value “World” in the string in Java

In this example, we take two strings: str and value; and find the last index of the value in the string str using String lastIndexOf() method.

Java Program

</>
Copy
public class Main {
	public static void main(String[] args) {
		String str = "Hello World! Happy World.";
		int index = str.lastIndexOf("World");
		System.out.println("Last index : " + index);
	}
}

Output

Last index : 19

Explanation

Hello World! Happy World.    ← given string
                   ↑
                   World     ← value to search for in the string
01234...        ...19         ← indices of characters in the string
                   ↑
                   19 is the index of last occurrence of search value

2. lastIndexOf() – Find the last index of “World” in the string when searching from an index of 15 in Java

In this example, we take two strings: str and value; and find the index of last occurrence of the value in the string str when the search happens from an index of 15, using String lastIndexOf() method. The value is searched in the string from index=15 towards index=0. Even if there is an occurrence of the value in the string after the given fromIndex, that occurrence is ignored.

Java Program

</>
Copy
public class Main {
	public static void main(String[] args) {
		String str = "Hello World! Happy World.";
		int index = str.lastIndexOf("World", 15);
		System.out.println("Last index : " + index);
	}
}

Output

Last index : 6

Explanation

Hello World! Happy World.    ← given string
               ↑
               15 is fromIndex - from where the search begins
      World  ← value to search for in the string
01234567...  ← indices
      ↑
      6 is the last index of search value

3. lastIndexOf() – When the search value is not present in the string

In this example, we take two strings: str and value; such that the value is not present in the string str, and observe the return value of lastIndexOf() method.

Java Program

</>
Copy
public class Main {
	public static void main(String[] args) {
		String str = "Hello World";
		int index = str.lastIndexOf("Apple");
		System.out.println("Last index : " + index);
	}
}

Output

Last index : -1

Since the given search value "Apple" is not present in the string "Hello World", the lastIndexOf() method returns -1.

4. lastIndexOf() – Find the last index of character ‘W’ in the string in Java

In this example, we will find the index of last occurrence of the character 'W' in the string str using String lastIndexOf() method.

You may wonder that how could we give a character value ‘W’ as argument to lastIndexOf() method, when it takes only a string or int value. Please notice that the char value can be implicitly converted to respective integer value. For example, ‘W’ would be converted to an integer value of 87.

Let us go ahead and find the index of last occurrence of 'W' in the string.

Java Program

</>
Copy
public class Main {
	public static void main(String[] args) {
		String str = "Wonder World.";
		int index = str.lastIndexOf('W');
		System.out.println("Last index : " + index);
	}
}

Output

Last index : 7

Explanation

W o n d e r   W o r l d    ← given string
              ↑
              W            ← value to search for in the string
0 1 2 3 4 5 6 7 8 9 10     ← indices of characters in the string
              ↑
              7   is where the value is found for last time in the string
              7   is the return value

Conclusion

In this Java String Methods tutorial, we have seen about String lastIndexOf() method in Java, its syntax, and how to use String lastIndexOf() method in Java programs with the help of examples.