In StringBuilder class, there are two types of lastIndexOf() method depending upon the parameters passed to it.
lastIndexOf(String str)
ThelastIndexOf(String str) method ofStringBuilder 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 parameterstr which is String type value refers to the String whose index of last occurrence we want to get.
Return Value: This method returnsthe 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.classGFG{publicstaticvoidmain(String[]args){// create a StringBuilder object// with a String pass as parameterStringBuilderstr=newStringBuilder("GeeksForGeeks");// print stringSystem.out.println("Stringcontains="+str);// get index of string Geeksintindex=str.lastIndexOf("Geeks");// print resultsSystem.out.println("Indexoflastoccurrence"+"string\"Geeks\"="+index);}}Output:String contains = GeeksForGeeksIndex 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.classGFG{publicstaticvoidmain(String[]args){// create a StringBuilder object// with a String pass as parameterStringBuilderstr=newStringBuilder("GeeksforGeekscontribute");// print stringSystem.out.println("Stringcontains="+str);// get index of string articleintindex=str.lastIndexOf("article");// print resultsSystem.out.println("Indexofstring"+"'article'="+index);}}Output:String contains = Geeks for Geeks contributeIndex of string 'article' = -1
lastIndexOf(String str, int fromIndex)
ThelastIndexOf(String str, int fromIndex) method ofStringBuilder 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 indexfromIndex. 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 returnsthe 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.classGFG{publicstaticvoidmain(String[]args){// create a StringBuilder object// with a String pass as parameterStringBuilderstr=newStringBuilder("WeGeeksLoveGeeksForGeeks");// print stringSystem.out.println("Stringcontains="+str);// get index of last occurrence of substring till 15th position of original Stringintindex=str.lastIndexOf("Geeks",15);// print resultsSystem.out.println("indexoflastoccurrence"+"string\"Geeks\"="+index);}}Output:String contains = WeGeeksLoveGeeksForGeeksindex 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.classGFG{publicstaticvoidmain(String[]args){// create a StringBuilder object// with a String pass as parameterStringBuilderstr=newStringBuilder("GeeksforGeekscontribute");// print stringSystem.out.println("Stringcontains="+str);// get index of string contribute from index 10intindex=str.lastIndexOf("contribute",10);// print resultsSystem.out.println("indexofstring"+"'contribute'="+index);}}Output:String contains = Geeks for Geeks contributeindex of string 'contribute ' = -1
References:
Explore
Java Basics
OOP & Interfaces
Collections
Exception Handling
Java Advanced
Practice Java