java.lang.String lastIndexOf()

Description :

This java tutorial shows how to use the lastIndexOf() method of java.lang.String class. This method returns an int datatype which which corresponds to the last index of the string where the method argument str has been found.

Method Syntax :

public int lastIndexOf(String str)

public int lastIndexOf(String str, int fromIndex)

Parameter Input :

[table]
DataType|Parameter|Description
String|str|the string argument which we want to check the position on our String
int|fromIndex|index on our String where we want to start searching
[/table]

Method Returns :

This method returns an int datatype which corresponds to the first occurrence index has been found. Note that since we are dealing with String, the index starts at 0. By default the last occurrence of  string “” is at the index equivalent of String length. If there if the specified parameter has not been found on our String then the result would be -1.

Java Code Example :

This example source code demonstrates the use of lastIndexOf() method of String class. Basically this source code just prints the last index where the input parameter str has been found.

Sample Output :

Running the lastIndexOf() method example source code of java.lang.String class will give you the following output

String lastIndexOf method example

String lastIndexOf method example

Suggested Reading List :