Published Aug 22, 2022Updated Aug 22, 2022
Contribute to Docs

The .indexOf() method returns the index of the first occurrence of a substring in a StringBuilder. If the substring is not found, -1 is returned.


myStringBuilder.indexOf(substring, index);

The argument substring is of type String.

The index is an int argument that is optional. If provided, .indexOf() returns the first occurrence of substring after that index in the StringBuilder.


The following example creates a StringBuilder with a specified string then uses .indexOf() to find the index of a particular substring:

import java.util.*;
public class Example {
public static void main(String[] args)
StringBuilder str = new StringBuilder("Hello World!");

This produces the following output:

Hello World!

All contributors

Looking to contribute?

Learn Java on Codecademy