Java StringBuilder lastIndexOf() Method Example

Java StringBuilder lastIndexOf() method has 2 overloaded versions. Here are all of its forms:
  1. int lastIndexOf(String str) - Returns the index within this string of the rightmost occurrence of the specified substring.
  2. int lastIndexOf(String str, int fromIndex) - Returns the index within this string of the last occurrence of the specified substring.

Java StringBuilder lastIndexOf() Method Example

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

Reference



Comments