Java StringBuilder.lastIndexOf() – Examples

In this tutorial, we will learn about the Java StringBuilder.lastIndexOf() function, and learn how to use this function to find the index of last occurrence of given string in this StringBuilder sequence, with the help of examples.

lastIndexOf(String str)

StringBuilder.lastIndexOf() returns the index within this string of the last occurrence of the specified substring.

ADVERTISEMENT

Syntax

The syntax of lastIndexOf() function is

lastIndexOf(String str)

where

ParameterDescription
strThe string to search for, in StringBuilder sequence.

Returns

The function returns an integer.

Example 1 – lastIndexOf(str)

In this example, we will create a StringBuilder with some initial value. We will then find the index of last occurrence of the string “abc” in the StringBuilder sequence using lastIndexOf() method.

Java Program

public class Example { 
    public static void main(String[] args) { 
        StringBuilder stringBuilder = new StringBuilder("abcdefabcdefabcdef");
        String str = "abc";

        int index = stringBuilder.lastIndexOf(str);
        System.out.println("Last index is : " + index);
    }
}

Output

Last index is : 12

Example 2 – lastIndexOf(str) – str not present

In this example, we will take a string, str, such that it is not present in StringBuilder sequence. In such case, StringBuilder.lastIndexOf(str) should return -1.

Java Program

public class Example { 
    public static void main(String[] args) { 
        StringBuilder stringBuilder = new StringBuilder("abcdefabcdefabcdef");
        String str = "abd";

        int index = stringBuilder.lastIndexOf(str);
        System.out.println("Last index is : " + index);
    }
}

Output

Last index is : -1

lastIndexOf(String str, int fromIndex)

StringBuilder.lastIndexOf() returns the index within this StringBuilder sequence, of the last occurrence of the specified string, searching backward starting at the specified fromIndex.

In other words, search has to happen until given fromIndex in the StringBuilder sequence.

Syntax

The syntax of lastIndexOf() function is

lastIndexOf(String str, int fromIndex)

where

ParameterDescription
strThe string to search for, in StringBuilder sequence.
fromIndexThe index in StringBuilder sequence, from which search has to happen towards the left.

Returns

The function returns an integer.

Example 3 – lastIndexOf(str, fromIndex)

In this example, we will take a string and find its last occurrence in a StringBuilder, where the search has to happen only until the index 10.

Java Program

public class Example { 
    public static void main(String[] args) { 
        StringBuilder stringBuilder = new StringBuilder("abcdefabcdefabcdef");
        String str = "abc";
        int fromIndex = 10;
        
        int index = stringBuilder.lastIndexOf(str, fromIndex);
        System.out.println("Last index is : " + index);
    }
}

Output

Last index is : 6

Example 4 – lastIndexOf(str, fromIndex)

In this example, we will take an index and string, such that string has no occurrence in the subsequence. StringBuilder.lastIndexOf(str, fromIndex) should return -1.

Java Program

public class Example { 
    public static void main(String[] args) { 
        StringBuilder stringBuilder = new StringBuilder("abcdefabcdefabcdef");
        String str = "ef";
        int fromIndex = 3;
        
        int index = stringBuilder.lastIndexOf(str, fromIndex);
        System.out.println("Last index is : " + index);
    }
}

Output

Last index is : -1

Conclusion

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