lastIndexOf(): This method is available in java.lang.StringBuffer class of Java.
Syntax:
int java.lang.StringBuffer.lastIndexOf(String str)
This method takes one argument of type String as its parameter. This method returns the index within this string of the last occurrence of the specified substring.
Note: The last occurrence of the empty string "" is considered to occur at the index value this.length().
Parameters: One parameter is required for this method.
str: the substring to search for.
Returns: the index of the last occurrence of the specified substring, or -1 if there is no such occurrence.
For Example:
StringBuffer str = new StringBuffer("Hello World")
String str1 = "l"
str.lastIndexOf(str1) = > It returns 9.
Approach
Java
public class LastIndexOf {public static void main(String[] args) {StringBuffer str = new StringBuffer("Hello World");String str1 = "l";System.out.println(str.lastIndexOf(str1));}}
Output:
9
No comments:
Post a Comment