Java StringBuffer lastIndexOf() method has 2 overloaded versions. All its forms:
- int lastIndexOf(String str) - Returns the index within this string of the rightmost occurrence of the specified substring.
- int lastIndexOf(String str, int fromIndex) - Returns the index within this string of the last occurrence of the specified substring.
Example: This example demonstrates the usage of 2 lastIndexOf() overloaded methods.
public class LastIndexOfExample {
public static void main(String[] args) {
StringBuffer buffer = new StringBuffer("javaguides");
// method1
int lastIndexOf = buffer.lastIndexOf("guides");
System.out.println(" last index of given string 'guides' in' "
+ buffer.toString()+"' :: " + lastIndexOf);
// method 2
lastIndexOf = buffer.lastIndexOf("java", 3);
System.out.println(" last index of given string 'java' in' "
+ buffer.toString()+"' :: " + lastIndexOf);
}
}
Output:
last index of given string 'guides' in' javaguides' :: 4
last index of given string 'java' in' javaguides' :: 0
Comments
Post a Comment