Java String lastIndexOf() Method with Example | Definition, Syntax, Usage & Types of Java.lang.String.lastIndexOf() Method

In the last tutorial, we have learned all about Java String indexOf() method. We discussed all the four variants of the indexOf() method. In this tutorial, we will learn about the Java String lastIndexOf() method. There are four variants of the lastIndexOf() method. Here, we will discuss all the four variants of the Java String lastIndexOf() method. Let’s discuss these four variants one by one.

Java.lang.String.lastIndexOf() Method

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

Internal Implementation

public int lastIndexOf(int ch) {  
       return lastIndexOf(ch, value.length - 1);  
   }

Parameters Values

  • ch: char value i.e. a single character e.g. ‘a’
  • fromIndex: index position from where the index of the char value or substring is returned
  • substring: substring to be explored in this string

Return Value

This method returns the last index of the string.

Read More: 

Java String lastIndexOf() Example

public class JavaExample {  
   public static void main(String[] args) {  
    String str = "BtechGeeks is for beginners";
    char ch = 'b';
    char ch2 = 's';
    String subStr = "beginners";
    int posOfB = str.lastIndexOf(ch);
    int posOfS = str.lastIndexOf(ch2);
    int posOfSubstr = str.lastIndexOf(subStr);
    System.out.println(posOfB);
    System.out.println(posOfS);
    System.out.println(posOfSubstr);
   }  
}

Java String lastIndexOf() Signature

You can observe 4 types of the lastIndexOf method in java. The signature of lastIndexOf methods are tabulated 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 an 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 the index

Java String lastIndexOf() Method with Example

1. lastIndexOf(int ch)

This is the first variant of the Java String lastIndexOf() method. This method returns the index of the last occurrence of the specified character. If the specified character is not present there, then it will return -1.

Syntax:

The syntax of the first variant of Java String lastIndexOf() method is given below:

public String lastIndexOf(int ch)

Here, ch is a character whose index we want to return.

Java String lastIndexOf(int ch) Method Example

class lastIndexOf{
public static void main(String args[]){
String str = "String tutorial by Btechgeeks";
//returns the last index
//of specified character.
System.out.println(str.lastIndexOf('i')); //12
}
}

Output:

12

2. lastIndexOf(int ch, int fromIndex)

This is the second variant of the Java String lastIndexOf() method. This method returns the index of the first occurrence of the specified character, starting the search from the backward specified index. This method returns -1 if the specified character is not present there.

Syntax:

The syntax of the second variant of Java String lastIndexOf() method is given below:

public String lastIndexOf(int ch, int fromIndex)

Here, ch is a character and fromIndex is the index position at which point the search begins.

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

class lastIndexOf{
public static void main(String args[]){
String str = "String tutorial by btechgeeks";
//returns the last index
//of 'i' before 15th index.
System.out.println(str.lastIndexOf('i', 15)); //12
}
}

Output:

12

3. lastIndexOf(String str)

This is the third variant of the Java String lastIndexOf() method. This method returns the index of the last occurrence of the specified substring. If the specified substring is not present there, it will return -1.

Syntax:

The syntax of the third variant of Java String lastIndexOf() method is given below:

public String lastIndexOf(String str)

Here, str is a substring whose index we want to return

Java String lastIndexOf(String str) Method Example

class lastIndexOf{
public static void main(String args[]){
String str = "String tutorial in java by btechgeeks";
//returns the last index of
//initial character of substring.
System.out.println(str.lastIndexOf("java")); //20
}
}

Output:

20

4. lastIndexOf(String str, int fromIndex)

This is the fourth variant of the Java String lastIndexOf() method. This method returns the index of the first occurrence of the specified substring, starting the search from the backward specified index. This method returns -1 if the specified substring is not present there.

Syntax:

The syntax of the fourth variant of Java String lastIndexOf() method is given below:

public String lastIndexOf(String str, int fromIndex)

Here, str is a substring and fromIndex is the index position at which point the search begins.

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

class lastIndexOf{
public static void main(String args[]){
String str = "String tutorial in java by btechgeeks";
//returns the last index of
//initial character of substring
//before index 30.
System.out.println(str.lastIndexOf("java", 30)); //20
}
}

Output:

20