StringBuffer indexOf() index in Java

indexOf(): This method is available in java.lang.StringBuffer class of Java.

Syntax:

int java.lang.StringBuffer.indexOf(String str, int fromIndex)

This method takes two arguments, one of type String and another of type int as its parameters. This method returns the index within this string of the first occurrence of the specified substring, starting at the specified index.

Parameters: Two parameters are required for this method.

str: the substring to search for.

fromIndex: the index from which to start the search.

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

For Example:

StringBuffer str = new StringBuffer("Hello World")

String str1 = "l"

int fromIndex = 5

str.indexOf(str1, fromIndex) = > It returns 9.

Approach

Java

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

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

        String str1 = "l";
        int fromIndex = 5;
        System.out.println(str.indexOf(str1, fromIndex));
    }
}

Output:

9

No comments:

Post a Comment