String indexOf operation

String.indexOf() returns the index of the first occurrence of a character or substring within a string, or -1 if not found. Java provides four indexOf() overloads, with and without a fromIndex starting position, plus matching lastIndexOf() variants that search from the end. This article covers all overloads with practical examples.

If you want to find first occurrence of a character in a String or first occurrence of a substring or first occurrences of these starting from a specific position index String class in Java has inbuilt functions for these usacases. In this article, we are going to looking at these functions with examples.

Contents

Index of a character in a String where it is first appeared

To find the first occurrence of a character, we can use String's String.indexOf(int ch) method. We can pass the character that we want to know first occurrence of, and it returns the index of first character occurrence or -1 if the character is not present in the string.

Note: As you may have noticed that String.indexOf(int ch) takes integer, but we can pass char as well, Java automatically converts that to Unicode value.

Example 1
Input: str = "HelloWorld", character to find = 'W'
Output: 5
Explanation: 'W' is at 5th index (starting from 0th index)
Example 2
Input: str = "HelloWorld", character to find = 'z'
Output: -1
Explanation: 'z' is not present in input string.
Java
public class StringIndexOf {

    public static void main(String[] args) {
        String str = "HelloWorld";
        int firstOccurrence  = str.indexOf('W');
        System.out.println("First occurrence of 'W' is : " +firstOccurrence);

        firstOccurrence  = str.indexOf('z');
        System.out.println("First occurrence of 'z' is : " +firstOccurrence);

    }
}

Running it prints:

Output
First occurrence of 'W' is : 5 
        First occurrence of 'z' is : -1

Index of a character in a String where it is first appeared, search from certain position index

To find the first occurrence of a character after a certain position index, we can use String's String.indexOf(int ch, int fromIndex) method. We can pass the character that we want to know first occurrence of, and the fromIndex is the begin index where the search should start, and it returns the index of first occurrence starting from 0th index and not from the fromIndex.

Example
Input: str = "HelloWorld" , character to find ='W', search begin index =4
Output: 5
Explanation: 'W' is at 5th index, we start search from 4th index and the letter 'W' is found at index 5 (starting from index 0, not a relative index from where the search begins).
Java
public class StringIndexOf {

    public static void main(String[] args) {
        String str = "HelloWorld";
        int firstOccurrenceAfterPos  = str.indexOf('W', 4);
        System.out.println("First occurrence of 'W' after position 4 is : " +firstOccurrenceAfterPos);

        firstOccurrenceAfterPos  = str.indexOf('W', 6);
        System.out.println("First occurrence of 'W' after position 6 is : " +firstOccurrenceAfterPos);
    }
}

Running it prints:

Output
First occurrence of 'W' after position 4 is : 5 
        First occurrence of 'W' after position 6 is : -1

Index of a substring in a String where it is first appeared

To find the first occurrence of a substring, we can use String's String.indexOf(String str) method, where str is the substring that we want to search for, and it returns the index of first occurrence of substring or -1 if the substring is not present in the string.

Example 1
Input: str = "HelloWorld", substring to find = "World"
Output: 5
Explanation: "World" substring starts at 5th index.
Example 2
Input: str = "HelloWorld", substring to find = "Hello"
Output: 0
Explanation: "Hello" substring starts at 0th index (beginning of the string).
Java
public class StringIndexOf {

    public static void main(String[] args) {
        String str = "HelloWorld";

        /* First occurrence of a substring */
        int firstOccurrenceSubStr  = str.indexOf("World");
        System.out.println("First occurrence of 'World' substring : " +firstOccurrenceSubStr);

        firstOccurrenceSubStr  = str.indexOf("Hello");
        System.out.println("First occurrence of 'Hello' substring : " +firstOccurrenceSubStr);
    }
}

Running it prints:

Output
First occurrence of 'World' substring : 5 
        First occurrence of 'Hello' substring : 0

Index of a substring in a String where it is first appeared, search from certain position index

To find the first occurrence of a substring after a certain position index, we can use String's String.indexOf(String str, int fromIndex) method, where str is the substring that we want to know first occurrence of, and the fromIndex is the begin index where the search should start, and it returns the index of first occurrence starting from 0th index and not from the fromIndex.

Example
Input: str = "HelloWorld" , substring to find ="World", search begin index =4
Output: 5
Explanation: "World" is at 5th index, we start search from 4th index and the substring "World" is found at index 5 (starting from index 0, not a relative index from where the search begins).
Java
public class StringIndexOf {

    public static void main(String[] args) {
        String str = "HelloWorld";

        /* First occurrence of a substring */
        int firstOccurrenceSubStrAfterPos  = str.indexOf("World", 4);
        System.out.println("First occurrence of 'World' substring after position 4 : " +firstOccurrenceSubStrAfterPos);

    }
}

Running it prints:

Output
First occurrence of 'World' substring after position 4 : 5

Above example complete source code can be found at GitHub