Thursday, July 4, 2024
HomeLanguagesJavaStringBuilder lastIndexOf() method in Java with Examples

StringBuilder lastIndexOf() method in Java with Examples

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

lastIndexOf(String str)

The lastIndexOf(String str) method of StringBuilder 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.StringBuilder.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 StringBuilder object
        // with a String pass as parameter
        StringBuilder str
            = new StringBuilder("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 StringBuilder object
        // with a String pass as parameter
        StringBuilder str
            = new StringBuilder(
                "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 StringBuilder class is the inbuilt method used to return the index of a substring within the original String. But the search for the substring begins from 0 index to the index fromIndex. In this new range (0-fromIndex), now the last occurrence of the substring is found and the starting index is returned by this function. If the substring isn’t found in that range, -1 is returned. Note that the starting point of the range where the last occurrence of the substring will be searched is always zero. The user can only set the ending point. 

Syntax:

public int lastIndexOf(String str, int fromIndex)

Parameters: This method accepts two one parameters:

  • str which is String type value refers to the substring whose index we’re trying to 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 StringBuilder.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 StringBuilder object
        // with a String pass as parameter
        StringBuilder str
            = new StringBuilder("WeGeeksLoveGeeksForGeeks");
 
        // print string
        System.out.println("String contains = " + str);
 
        // get index of last occurrence of substring till 15th position of original String
        int index = str.lastIndexOf("Geeks", 15);
 
        // print results
        System.out.println("index of last occurrence"
                           + " string \"Geeks\" = "
                           + index);
    }
}


Output:

String contains = WeGeeksLoveGeeksForGeeks
index of last occurrence string "Geeks" = 11

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 StringBuilder object
        // with a String pass as parameter
        StringBuilder str
            = new StringBuilder("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:

Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments