String.indexOf()
method in Java is used to find the index of a specified character or substring within a string. This guide will cover the method's usage, explain how it works, and provide examples to demonstrate its functionality.Table of Contents
- Introduction
indexOf
Method Syntax- Examples
- Finding a Character's Index
- Finding a Substring's Index
- Starting Search from a Specific Index
- Handling Not Found Cases
- Conclusion
Introduction
The String.indexOf()
method is a member of the String
class in Java. It allows you to locate the position of a character or substring within a string. This method is particularly useful for searching, parsing, and manipulating strings based on specific characters or patterns.
indexOf Method Syntax
The indexOf
method has several overloads:
- Finding the index of a character:
public int indexOf(int ch)
- Finding the index of a character starting from a specified index:
public int indexOf(int ch, int fromIndex)
- Finding the index of a substring:
public int indexOf(String str)
- Finding the index of a substring starting from a specified index:
public int indexOf(String str, int fromIndex)
Examples
Finding a Character's Index
The indexOf
method can be used to find the first occurrence of a specified character within a string.
Example
public class IndexOfExample {
public static void main(String[] args) {
String message = "Hello, World!";
int indexOfH = message.indexOf('H');
int indexOfW = message.indexOf('W');
System.out.println("Index of 'H': " + indexOfH);
System.out.println("Index of 'W': " + indexOfW);
}
}
Output:
Index of 'H': 0
Index of 'W': 7
Finding a Substring's Index
The indexOf
method can be used to find the first occurrence of a specified substring within a string.
Example
public class IndexOfExample {
public static void main(String[] args) {
String message = "Hello, World!";
int indexOfHello = message.indexOf("Hello");
int indexOfWorld = message.indexOf("World");
System.out.println("Index of 'Hello': " + indexOfHello);
System.out.println("Index of 'World': " + indexOfWorld);
}
}
Output:
Index of 'Hello': 0
Index of 'World': 7
Starting Search from a Specific Index
The indexOf
method can be used to start the search from a specified index.
Example
public class IndexOfExample {
public static void main(String[] args) {
String message = "Hello, World! Hello, Java!";
int indexOfHelloFirst = message.indexOf("Hello");
int indexOfHelloSecond = message.indexOf("Hello", 10);
System.out.println("Index of first 'Hello': " + indexOfHelloFirst);
System.out.println("Index of second 'Hello': " + indexOfHelloSecond);
}
}
Output:
Index of first 'Hello': 0
Index of second 'Hello': 14
Handling Not Found Cases
The indexOf
method returns -1
if the specified character or substring is not found.
Example
public class IndexOfExample {
public static void main(String[] args) {
String message = "Hello, World!";
int indexOfJava = message.indexOf("Java");
System.out.println("Index of 'Java': " + indexOfJava);
}
}
Output:
Index of 'Java': -1
Conclusion
The String.indexOf()
method in Java for locating the position of characters and substrings within a string. By understanding how to use this method, you can efficiently search, parse, and manipulate strings in your Java applications. Whether you are finding the index of a character, a substring, starting the search from a specific index, or handling cases where the character or substring is not found, the indexOf
method provides a reliable solution for these tasks.
Comments
Post a Comment
Leave Comment