Java StringBuffer lastIndexOf() Example

Java StringBuffer lastIndexOf() method has 2 overloaded versions. All 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 StringBuffer lastIndexOf() Example

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

Reference



Comments