Open In App

StringBuffer lastIndexOf() method in Java with Examples

Last Updated : 18 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In StringBuffer class, there are two types of lastIndexOf() method depending upon the parameters passed to it.

lastIndexOf(String str)

The lastIndexOf(String str) method of StringBuffer class is the inbuilt method used to return the index within the String for last occurrence of passed substring as parameter. The last occurrence of the empty string “” is considered to occur at the index value this.length(). If substring str is not present then -1 is returned. 

Syntax:

public int lastIndexOf(String str)

Parameters: This method accepts only one parameter str which is String type value refers to the String whose index of last occurrence we want to get. 

Return Value: This method returns the index of the last occurrence of the passed substring, or -1 if there is no such substring present. 

Below programs illustrate the java.lang.StringBuffer.lastIndexOf() method: 

Example 1: When passed substring is present in the sequence. 

Java




// Java program to demonstrate
// the lastIndexOf() Method.
 
class GFG {
    public static void main(String[] args)
    {
 
        // create a StringBuffer object
        // with a String pass as parameter
        StringBuffer str
            = new StringBuffer("GeeksForGeeks");
 
        // print string
        System.out.println("String contains = " + str);
 
        // get index of string Geeks
        int index = str.lastIndexOf("Geeks");
 
        // print results
        System.out.println("Index of last occurrence"
                           + " string \"Geeks\"= "
                           + index);
    }
}


Output:

String contains = GeeksForGeeks
Index of last occurrence string "Geeks"= 8

Example 2: when passed substring is not present in the sequence. 

Java




// Java program to demonstrate
// the lastIndexOf() Method.
 
class GFG {
    public static void main(String[] args)
    {
 
        // create a StringBuffer object
        // with a String pass as parameter
        StringBuffer str
            = new StringBuffer(
                "Geeks for Geeks contribute");
 
        // print string
        System.out.println("String contains = " + str);
 
        // get index of string article
        int index = str.lastIndexOf("article");
 
        // print results
        System.out.println("Index of string"
                           + " 'article' = "
                           + index);
    }
}


Output:

String contains = Geeks for Geeks contribute
Index of string 'article' = -1

lastIndexOf(String str, int fromIndex)

The lastIndexOf(String str, int fromIndex) method of StringBuffer class is the inbuilt method used to return the index within the String for first occurrence of passed substring as parameter, searching backward starting at the specified index ‘fromIndex’. If substring str is not present then -1 is returned. fromIndex is Integer type value refers to the index from which to start the search but this search is backward from index ‘fromIndex’. The index returned by this method is calculated from the start of the sequence only difference is that the index of the start of the search is given in this method. if string present after the index of the start of search but not before then -1 will return. 

Syntax:

public int lastIndexOf(String str, int fromIndex)

Parameters: This method accepts two one parameters:

  • str which is String type value refers to the String whose index is to be get
  • fromIndex which is Integer type value refers to the index from which to start the search backwards.

Returns: This method returns the index of the last occurrence of the passed substring starting at the specified index, or -1 if there is no such substring present. 

Below programs illustrate the StringBuffer.lastIndexOf() method: 

Example 1: when passed substring is present in the sequence. 

Java




// Java program to demonstrate
// the lastIndexOf() Method.
 
class GFG {
    public static void main(String[] args)
    {
 
        // create a StringBuffer object
        // with a String pass as parameter
        StringBuffer str
            = new StringBuffer("GeeksForGeeks");
 
        // print string
        System.out.println("String contains = " + str);
 
        // get index of string For from index 8
        int index = str.lastIndexOf("For", 8);
 
        // print results
        System.out.println("index of last occurrence"
                           + " string \"For\" = "
                           + index);
    }
}


Output:

String contains = GeeksForGeeks
index of last occurrence string "For" = 5

Example 2: when the fromIndex is < the last occurrence index of substring 

Java




// Java program to demonstrate
// the lastIndexOf() Method.
 
class GFG {
    public static void main(String[] args)
    {
 
        // create a StringBuffer object
        // with a String pass as parameter
        StringBuffer str
            = new StringBuffer("Geeks for Geeks contribute");
 
        // print string
        System.out.println("String contains = " + str);
 
        // get index of string Contribute from index 10
        int index = str.lastIndexOf("contribute", 10);
 
        // print results
        System.out.println("index of string"
                           + " 'contribute ' = "
                           + index);
    }
}


Output:

String contains = Geeks for Geeks contribute
index of string 'contribute ' = -1

References:



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads