What is Java String indexOf()?
As a Java developer, you must have come across the need to find the position of a character or a substring within a string. Java provides a built-in method called indexOf() in the String class to help us with this task. The indexOf() method in Java is a part of the String class, i.e. Java String class indexOf(), which is used to manipulate strings. The indexOf() method in Java returns the position of the first occurrence of a character or a substring within a given string. If the character or substring is not found, the method returns -1.
// Here is the syntax of the String indexOf():
public int indexOf(int ch)
public int indexOf(int ch, int fromIndex)
public int indexOf(String str)
public int indexOf(String str, int fromIndex)
Let's dive into the different ways in which we can use the `indexOf()` method.
Finding the position of a character in the string:
Suppose we have a string "Hello, world!". To find the position of the character 'o' in the string, we can use the following code:
public class StringIndexOfExample {
public static void main(String[] args) {
String str = "Hello, world!";
// We will use Str.indexOf here
int position = str.indexOf('o');
System.out.println("Position of 'o' in the string is: " + position);
}
}
Output
Position of 'o' in the string is: 4
In the above code, we first declared a string "Hello, world!" and then used the indexOf() method to find the position of the character 'o' in the string. The method returned position 4, which is the first occurrence of the character 'o' in the string.
Finding the position of characters in a string
Here's an example code snippet that demonstrates how to use the indexOf() method to find the position of characters in a string:
public class StringIndexOfExample {
public static void main(String[] args) {
String str = "Hello, world!";
// We will use Str.indexOf here
int position = str.indexOf('o');
while (position >= 0) {
System.out.println("'o' found at position " + position);
position = str.indexOf('o', position + 1);
}
}
}
Output
'o' found at position 4
'o' found at position 8
In the above code, we first declared a string "Hello, world!" and then used the indexOf() method to find the position of the character 'o' in the string. The indexOf() method returns the position of the first occurrence of the character in the string, or -1 if the character is not found.
We then used a while loop to continue searching for the character 'o' in the string by calling indexOf() with the character and the next starting position as arguments. We repeat this process until indexOf() returns -1, indicating that there are no more occurrences of the character in the string.
In this example, we found that the character 'o' appears at positions 4 and 8 in the string "Hello, world!".
Finding the position of a substring in the string
Suppose we have a string "Java is a popular programming language". To find the position of the substring "programming" in the string, we can use the following code:
public class StringIndexOfExample {
public static void main(String[] args) {
String str = "Java is a popular programming language";
// We will use Str.indexOf here
int position = str.indexOf("programming");
System.out.println("Position of 'programming' in the string is: " + position);
}
}
Output
Position of 'programming' in the string is: 18
In the above code, we first declared a string "Java is a popular programming language" and then used the indexOf() method to find the position of the substring "programming" in the string. The method returned the position 18, which is the starting position of the substring "programming" in the string.
Finding all occurrences of a character or substring in the string
Suppose we have a string "Java is a popular programming language. Java is widely used in web development". To find all occurrences of the character 'a' or the substring "Java" in the string, we can use the following code:
public class StringIndexOfExample {
public static void main(String[] args) {
String str = "Java is a popular programming language. Java is widely used in web development.";
int position = -1;
while ((position = str.indexOf('a', position + 1)) != -1) {
System.out.println("Found 'a' at position: " + position);
}
while ((position = str.indexOf("Java", position + 1)) != -1) {
System.out.println("Found 'Java' at position: " + position);
}
}
}
Output
Found 'a' at position: 1
Found 'a' at position: 3
Found 'a' at position: 8
Found 'a' at position: 15
Found 'a' at position: 23
Found 'a' at position: 31
Found 'a' at position: 35
Found 'a' at position: 41
Found 'a' at position: 43
Found 'Java' at position: 0
Found 'Java' at position: 40
In the above code, we first declared a string "Java is a popular programming language. Java is widely used in web development" and then used the indexOf() method to find all occurrences of the character 'a' or the substring "Java" in the string. We used a while loop to iterate over all occurrences of the character or substring until the indexOf() method returns -1, which indicates that there are no more occurrences of the character or substring in the string.
GO TO FULL VERSION