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
Contents
Index of a character in a String where it is first appeared Index of a character in a String where it is first appeared, search from certain position index Index of a substring in a String where it is first appeared Index of a substring in a String where it is first appeared, search from certain position index
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)index
Note:
As you may have noticed that String.indexOf(int ch)char
Example 1
Output: 5
Explanation: 'W' is at 5th index (starting from 0th index)
Example 2
Output: -1
Explanation: 'z' is not present in input string.
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:
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)fromIndexindexfromIndex
Example
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).
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:
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)strindex
Example 1
Output: 5
Explanation: "World" substring starts at 5th index.
Example 2
Output: 0
Explanation: "Hello" substring starts at 0th index (beginning of the string).
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:
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)strfromIndexindexfromIndex
Example
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).
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:
First occurrence of 'World' substring after position 4 : 5
Above example complete source code can be found at