The String.indexOf()
method in Java is used to find the index of the first occurrence of a specified character or substring within a string.
Table of Contents
- Introduction
indexOf
Method Syntax- Examples
- Basic Usage
- Finding the First Occurrence of a Character
- Finding the First Occurrence of a Substring
- Finding with a Start Index
- Handling Cases Where the Character or Substring is Not Found
- Real-World Use Case
- Conclusion
Introduction
The String.indexOf()
method is a member of the String
class in Java. It allows you to find the position of a character or substring within a string. If the character or substring is found, the method returns the index of its first occurrence. If it is not found, the method returns -1
.
indexOf() Method Syntax
The syntax for the indexOf
method is as follows:
Finding the First Occurrence of a Character
public int indexOf(int ch)
Finding the First Occurrence of a Character from a Specified Index
public int indexOf(int ch, int fromIndex)
Finding the First Occurrence of a Substring
public int indexOf(String str)
Finding the First Occurrence of a Substring from a Specified Index
public int indexOf(String str, int fromIndex)
Examples
Basic Usage
Finding the First Occurrence of a Character
public class IndexOfCharExample {
public static void main(String[] args) {
String str = "Welcome to Java";
int index = str.indexOf('J');
System.out.println("Index of 'J': " + index);
}
}
Output:
Index of 'J': 11
Finding the First Occurrence of a Substring
public class IndexOfSubstringExample {
public static void main(String[] args) {
String str = "Welcome to Java";
int index = str.indexOf("Java");
System.out.println("Index of 'Java': " + index);
}
}
Output:
Index of 'Java': 11
Finding with a Start Index
Finding the First Occurrence of a Character from a Specified Index
public class IndexOfCharFromIndexExample {
public static void main(String[] args) {
String str = "Welcome to Java";
int index = str.indexOf('o', 5);
System.out.println("Index of 'o' from index 5: " + index);
}
}
Output:
Index of 'o' from index 5: 9
Finding the First Occurrence of a Substring from a Specified Index
public class IndexOfSubstringFromIndexExample {
public static void main(String[] args) {
String str = "Welcome to Java, Welcome to Programming";
int index = str.indexOf("Welcome", 10);
System.out.println("Index of 'Welcome' from index 10: " + index);
}
}
Output:
Index of 'Welcome' from index 10: 21
Handling Cases Where the Character or Substring is Not Found
If the character or substring is not found, the indexOf
method returns -1
.
Example
public class IndexOfNotFoundExample {
public static void main(String[] args) {
String str = "Welcome to Java";
int index = str.indexOf('z');
System.out.println("Index of 'z': " + index);
}
}
Output:
Index of 'z': -1
Real-World Use Case
Example: Checking the Position of a Substring
One common use case for indexOf
is checking the position of a substring within a larger string, such as validating if a URL contains a specific domain.
public class ValidateUrlExample {
public static void main(String[] args) {
String url = "https://www.example.com/page";
if (url.indexOf("example.com") != -1) {
System.out.println("The URL contains 'example.com'.");
} else {
System.out.println("The URL does not contain 'example.com'.");
}
}
}
Output:
The URL contains 'example.com'.
In this example, the indexOf
method is used to check if the URL contains the specified domain.
Conclusion
The String.indexOf()
method in Java is used for finding the index of the first occurrence of a character or substring within a string. It provides various overloads to accommodate different use cases, including starting the search from a specific index. This method is particularly useful for validation, searching, and manipulation tasks in various applications. By understanding and utilizing the indexOf
method, you can efficiently manage string searches in your Java programs.