Site icon DataFlair

Java String indexOf() Method with Examples

java string indexof()

Get Job-ready: Java Course with 45+ Real-time Projects! - Learn Java

The indexOf() method in Java is a precious resource for handling and searching strings, serving as a means to identify the initial occurrence of a character or substring within a larger string. Its primary function is to provide the position of the first match found or return -1 if no match is detected. This method comes in four distinct variants, each tailored to specific search requirements:

The first variant, indexOf(char ch), is designed to locate the initial instance of a specified character, ‘ch,’ within the string. The second variant, indexOf(char ch, int fromIndex), serves a similar purpose but allows for the search to commence from a designated starting index, ‘fromIndex.’

On the other hand, the third variant, indexOf(String str), is employed to identify the first occurrence of a particular substring, ‘str.’ Finally, the fourth variant, indexOf(String str, int fromIndex), closely resembles the second variant, as it also permits the search to begin from a user-defined starting index, ‘fromIndex.’

The existence of these various forms of the indexOf() method underscores its flexibility and utility for string searching and manipulation in Java.

This article will explore the syntax and practical application of these indexOf() method variants using instructive examples, providing a comprehensive understanding of their functionality.

Variants of Java indexOf() Method

A. int indexOf(char ch)

This variant of indexOf() finds the first occurrence of the specified character ch in the string.

Syntax:

int indexOf(char ch)

Example:

public class Main {
    public static void main(String[] args) {
        String str = "Hello World";
        int index = str.indexOf('o');
        System.out.println(index);
    }
}

Output:
4

B. int indexOf(char ch, int fromIndex)

This overload of indexOf() finds the first occurrence of the specified character ch, starting the search from fromIndex.

Syntax:

int indexOf(char ch, int fromIndex)

Example:

public class Main {
    public static void main(String[] args) {
        String str = "Hello World";
        int index = str.indexOf('o', 5);
        System.out.println(index);
    }
}

Output:
7

C. int indexOf(String substring)

This variant finds the first occurrence of the specified substring in the string.

Syntax:

int indexOf(String substring)

Example:

public class Main {
    public static void main(String[] args) {
        String str = "Hello World";
        int index = str.indexOf("World");
        System.out.println(index);
    }
}

Output:
6

D. int indexOf(String substring, int fromIndex)

This overload finds the first occurrence of the specified substring starting the search from fromIndex.

Syntax:

int indexOf(String substring, int fromIndex)

Example:

public class Main {
    public static void main(String[] args) {
        String str = "Hello World";
        int index = str.indexOf("World", 7);
        System.out.println(index);
    }
}

Output:
-1

Conclusion

In conclusion, Java’s indexOf() method offers a versatile and efficient means for string searching and manipulation. Its four distinct variants allow developers to locate characters or substrings within a larger string easily. These variants, including the ability to specify the starting index for the search, add significant flexibility to the method’s functionality.

By exploring the syntax and examples of each variant, developers can understand how to effectively employ the indexOf() method in their Java programming endeavors.

Exit mobile version