Java StringBuffer indexOf() Example

There are two forms of indexOf() methods in StringBuffer class:
  • indexOf(String str) - Returns the index within this string of the first occurrence of the specified substring.
  • indexOf(String str, int fromIndex) - Returns the index within this string of the first occurrence of the specified substring, starting at the specified index.

Java StringBuffer indexOf() Example

Example: This is a simple program to demonstrate 2 indexOf() methods with different signatures.
public class IndexOfExample {
    public static void main(String[] args) {
        StringBuffer buffer = new StringBuffer("javaguides");
  
        // method 1
        int index = buffer.indexOf("guides");
        System.out.println(index);
  
        // method2
         index = buffer.indexOf("guides", 3);
        System.out.println(index);
    }
}
Output:
4
4

Reference



Comments