Home »
Java programming language
Java StringBuffer int lastIndexOf(String s , int srcindex) method with Example
Java StringBuffer int lastIndexOf(String s , int srcindex) method: Here, we are going to learn about the int lastIndexOf(String s , int srcindex) method of StringBuffer class with its syntax and example.
Submitted by Preeti Jain, on July 01, 2019
StringBuffer Class int lastIndexOf(String s , int srcindex)
- This method is available in package java.lang.StringBuffer.lastIndexOf(String s, int srcindex).
- This method is used to return the index of the last occurrence of the specified substring which will be searched in a StringBuffer object and srcindex(Searching index) is the index to start the search from.
- This method returns -1 if substring is not found in the given StringBuffer object.
Syntax:
int lastIndexOf(String s, int srcindex){
}
Parameter(s):
We pass only two objects in the method of the StringBuffer i.e. String (s) and searching index(srcindex). Here String is those characters sequence which we want to search and srcindex is the index to start the search from.
Return value:
The return type of this method is int that means this method returns the index of the last occurrence of specified substring which will be search in a StringBuffer object and i.e. index is in integer form that's why return type is int.
Java program to demonstrate example of lastIndexOf(String s , int srcindex) method
Case 1: How int lastIndexOf(String s, int srcindex) method works with duplicate(Multiple times) substring in the StringBuffer object?
import java.lang.StringBuffer;
public class StringBufferClass {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer(" java.utilisapackageofJava ");
// use lastIndexOf(String s , int srcindex)
// it will retrieve the index of last occurrence
// of specified substring from the StringBuffer object .
// Display result after implementing lastIndexOf("i""",9 )i.e.
// "i" comes twice till the searching of 9th index in a
// stringbuffer object so it returns the index of
// last occurrence of "i" i.e. 9th index
// [ First index of "i" is 7th index and Second index of "i" is 9th]
System.out.println("The index of last occurrence of i is :" + sb.lastIndexOf("i", 9));
}
}
Output
The index of last occurrence of i is :8
Case 2: How int lastIndexOf(String s) method works without duplicate(i.e. Multiple times) substring in the StringBuffer object?
import java.lang.StringBuffer;
public class StringBufferClass {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer("Javaispopularinthisworld ");
// use lastIndexOf(String s) it will retrieve
// the index of last occurrence of specified
// substring from the StringBuffer object .
// Display result after implementing lastIndexOf("u")
// i.e. u comes once till 9th index in a stringbuffer
// object so it returns the index of last occurrence of u
// i.e. 9th index[ First and Last index of u is one and only one 9th position]
System.out.println("The index of last occurrence of u is :" + sb.lastIndexOf("u", 9));
}
}
Output
The index of last occurrence of u is :9