The indexOf() method in Java is used to find the index of the first occurrence of a specified character or substring within a string. It returns the index (zero-based) of the first occurrence of the specified character or substring, or -1 if the character or substring is not found.
1. Find the index of a single character
Syntax:
public int indexOf(int ch)
ch: The character whose index you want to find.
Note: int: The method returns the index of the first occurrence of the character or substring. If the character or substring is not found, it returns -1.
Example:
public class Main {
public static void main(String[] args) {
String str = "Hello, World!";
// Find the index of the character 'e'
int indexOfe = str.indexOf('e');
// Find the index of the character 'W'
int indexOfW = str.indexOf('W');
// Find the index of the character 'x' (which doesn't exist in the string)
int indexOfx = str.indexOf('x');
System.out.println("Index of 'e': " + indexOfe); // Output: 1
System.out.println("Index of 'W': " + indexOfW); // Output: 7
System.out.println("Index of 'x': " + indexOfx); // Output: -1
}
}
2. Find the index of a substring
Syntax:
public int indexOf(String str)
str: The substring whose index you want to find.
Example:
public class Main {
public static void main(String[] args) {
String str = "Hello, World!";
// Find the index of the substring "World"
int indexOfWorld = str.indexOf("World");
// Find the index of the substring "How" (which doesn't exist)
int indexOfHow = str.indexOf("How");
System.out.println("Index of 'World': " + indexOfWorld); // Output: 7
System.out.println("Index of 'How': " + indexOfHow); // Output: -1
}
}
3. Find the index of a character from a specific position
Syntax:
indexOf(int ch, int fromIndex)
public class Main {
public static void main(String[] args) {
String str = "Hello, World!";
// Find the index of the character 'l' starting from index 4
int indexOfOFrom4 = str.indexOf('l', 4);
System.out.println("Index of 'l' from index 4: " + indexOfOFrom4); // Output: 10
}
}
Explanation:
The search for the character ‘l’ starts from index 2. The first ‘l’ that is found after index 4 is at index 10 (in the substring “World!”).
4. Find the index of a substring from a specific position
Syntax:
indexOf(String str, int fromIndex)
Example:
public class Main {
public static void main(String[] args) {
String str = "Hello, Hello, Friends!";
// Find the index of the substring "Hello" starting from index 7
int indexOfHelloFrom7 = str.indexOf("Hello", 7);
System.out.println("Index of 'Hello' from index 7: " + indexOfHelloFrom7); // Output: 13
}
}
Explain: The search for the substring “Hello” starts at index 7. The second occurrence of “Hello” starts at index 13.
5. Find the empty string
If the substring or character is an empty string, indexOf() will return 0 because an empty string is trivially found at the beginning of the string.
public class Main {
public static void main(String[] args) {
String str = "Hello, World!";
// Find the index of an empty string
int indexOfEmpty = str.indexOf("");
System.out.println("Index of empty string: " + indexOfEmpty); // Output: 0
}
}