Java StringBuilder indexOf() Method Example

Java StringBuilder provides two forms of indexOf() methods
  • 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 StringBuilder indexOf() Method Example

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

Reference


Comments