Java String lastIndexOf() method example

HQ » Java Tutorial » java.lang » String » lastIndexOf() method example
Java String lastIndexOf() method example 2018-02-19T12:56:45+00:00

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 :

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

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 :


Contact Info


Our Goal

JavaTutorialHQ aims to to be The Ultimate Guide on Java with hundreds of examples from basic to advance Topics.

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.