Java StringBuilder lastIndexOf() method has 2 overloaded versions. Here are all of 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.
This example demonstrates the usage of 2 lastIndexOf() overloaded methods.
public class LastIndexOfExample {
public static void main(String[] args) {
StringBuilder builder = new StringBuilder("javaguides");
// method1
int lastIndexOf = builder.lastIndexOf("guides");
System.out.println(" last index of given string 'guides' in' "
+ builder.toString() + "' :: " + lastIndexOf);
// method 2
lastIndexOf = builder.lastIndexOf("java", 3);
System.out.println(" last index of given string 'java' in' "
+ builder.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