StringBuilder lastIndexOf() in Java

lastIndexOf(): This method is available in java.lang.StringBuilder class of Java.

Syntax:

int java.lang.StringBuilder.lastIndexOf(String str)

This method takes one argument of type String as its parameter. This method returns the index within this string of the last occurrence of the specified substring.

Parameters: One parameter is required for this method.

str: the substring to search for.

Returns: the index of the last occurrence of the specified substring, or -1 if there is no such occurrence.

For Example:

StringBuilder str = new StringBuilder("Hello World")

String str1 = "l"

str.lastIndexOf(str1) = > It returns 9.

Approach

Java

public class LastIndexOf {
    public static void main(String[] args) {

        StringBuilder str = new StringBuilder("Hello World");

        String str1 = "l";
        System.out.println(str.lastIndexOf(str1));
    }
}

Output:

9

No comments:

Post a Comment