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
- 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
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.
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.
Input: str = "HelloWorld", character to find = 'W'
Output: 5
Explanation: 'W' is at 5th index (starting from 0th index)
Input: str = "HelloWorld", character to find = 'z'
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);
}
}
When you run the above program, you will get the below output.
First occurrence of 'W' is : 5
First occurrence of 'z' is : -1
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.
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).
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);
}
}
When you run the above program, you will get the below output.
First occurrence of 'W' after position 4 is : 5
First occurrence of 'W' after position 6 is : -1
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.
Input: str = "HelloWorld", substring to find = "World"
Output: 5
Explanation: "World" substring starts at 5th index.
Input: str = "HelloWorld", substring to find = "Hello"
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);
}
}
When you run the above program, you will get the below output.
First occurrence of 'World' substring : 5
First occurrence of 'Hello' substring : 0
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.
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).
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);
}
}
When you run the above program, you will get the below output.
First occurrence of 'World' substring after position 4 : 5
Above example complete source code can be found at Github