StringBuilder indexOf() index in Java

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

Syntax:

int java.lang.StringBuilder.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:

StringBuilder str = new StringBuilder("Hello World")

String str1 = "l"

int fromIndex = 5

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

Approach

Java

package com.example.stringBuilder;

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

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

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

Output:

9

No comments:

Post a Comment