Java StringBuilder.indexOf() – Examples

In this tutorial, we will learn about the Java StringBuilder.indexOf() function, and learn how to use this function to get index of first occurrence of specified substring within StringBuilder sequence, with the help of examples.

indexOf(String str)

StringBuilder.indexOf() finds and returns the index of first occurrence of specified substring within StringBuilder sequence.

If there is no occurrence of specified string, then indexOf() returns -1.

ADVERTISEMENT

Syntax

The syntax of indexOf() function is

indexOf(String str)

where

ParameterDescription
strThe substring to search for, in this StringBuilder sequence.

Returns

The function returns integer.

Example 1 – indexOf(str)

In this example, we will create a StringBuilder stringBuilder and initialize a string str. We will use indexOf(str) and find the index of first occurrence of this string in StringBuilder sequence.

Java Program

public class Example { 
    public static void main(String[] args) { 
        StringBuilder stringBuilder = new StringBuilder("abcdefghijklmn");
        String str = "def";
        
        int index = stringBuilder.indexOf(str);
        System.out.print("Index of string is : "+index);
    }
}

Output

Index of string is : 3

Example 2 – indexOf(str) – str not present

In this example, we will take a string such that it is not present in the StringBuilder sequence. If string is not present in this StringBuilder sequence, indexOf(str) returns -1.

Java Program

public class Example { 
    public static void main(String[] args) { 
        StringBuilder stringBuilder = new StringBuilder("abcdefghijklmn");
        String str = "xyz";
        
        int index = stringBuilder.indexOf(str);
        System.out.print("Index of string is : "+index);
    }
}

Output

Index of string is : -1

Example 3 – indexOf(str) – Multiple Occurrences

In this example, we will take a string such that it is present multiple times in the StringBuilder sequence. Even if string has multiple occurrences in this StringBuilder sequence, indexOf(str) returns only the index of first occurrence.

Java Program

public class Example { 
    public static void main(String[] args) { 
        StringBuilder stringBuilder = new StringBuilder("abcdefabcdefabcdef");
        String str = "cd";
        
        int index = stringBuilder.indexOf(str);
        System.out.print("Index of string is : "+index);
    }
}

Output

Index of string is : 2

indexOf(String str, int fromIndex)

StringBuilder.indexOf() returns the index of the first occurrence of the specified substring, starting at the specified index in StringBuilder sequence.

If there is no occurrence of specified string, then indexOf() returns -1.

Syntax

The syntax of indexOf() function is

indexOf(String str, int fromIndex)

where

ParameterDescription
strThe substring to search for, in this StringBuilder sequence.
fromIndexThe index from which to start the search in StringBuilder sequence.

Returns

The function returns integer.

Example 4 – indexOf(str, fromIndex)

In this example, we will create a StringBuilder stringBuilder and initialize a string str. We will use indexOf(str, fromIndex) to find the index of first occurrence of this string in StringBuilder sequence from given index.

Java Program

public class Example { 
    public static void main(String[] args) { 
        StringBuilder stringBuilder = new StringBuilder("abcdefabcdefabcdef");
        String str = "abc";
        int fromIndex = 2; 
        
        int index = stringBuilder.indexOf(str, fromIndex);
        System.out.print("Index of string is : "+index);
    }
}

Output

Index of string is : 6

Example 5 – indexOf(str, fromIndex) – str not present

In this example, we will take a string such that it is not present in the StringBuilder sequence from specified fromIndex. If string is not present in this StringBuilder sequence, indexOf(str) returns -1.

Java Program

public class Example { 
    public static void main(String[] args) { 
        StringBuilder stringBuilder = new StringBuilder("abcdefabcdefabcdef");
        String str = "xyz";
        int fromIndex = 2; 
        
        int index = stringBuilder.indexOf(str, fromIndex);
        System.out.print("Index of string is : "+index);
    }
}

Output

Index of string is : -1

Conclusion

In this Java Tutorial, we have learnt the syntax of Java StringBuilder.indexOf() function, and also learnt how to use this function with the help of examples.