Java String lastIndexOf()

The java string lastIndexOf() method returns last index of the given character value or substring. If it is not found, it returns -1. The index counter starts from zero.


Internal Implementation


Signature

There are 4 types of lastIndexOf method in java. The signature of lastIndexOf methods are given below:

No. Method Description
1 int lastIndexOf(int ch) returns last index position for the given char value
2 int lastIndexOf(int ch, int fromIndex) returns last index position for the given char value and from index
3 int lastIndexOf(String substring) returns last index position for the given substring
4 int lastIndexOf(String substring, int fromIndex) returns last index position for the given substring and from index

Parameters

ch: char value i.e. a single character e.g. 'a'

fromIndex: index position from where index of the char value or substring is retured

substring: substring to be searched in this string


Returns

last index of the string


Java String lastIndexOf() method example

Test it Now

Output:

6

Java String lastIndexOf(int ch, int fromIndex) Method Example

Here, we are finding last index from the string by specifying fromIndex

Test it Now

Output:

3

Java String lastIndexOf(String substring) Method Example

It returns the last index of the substring.

Test it Now

Output:

19

Java String lastIndexOf(String substring, int fromIndex) Method Example

It returns the last index of the substring from the fromIndex.

Test it Now

Output:

19
-1




Contact US

Email:[email protected]

String lastIndexOf()
10/30