String indexOf() Method in Java
The indexOf() method in Java is a built-in method
provided by the String class that is used to find the index (position) of the
first occurrence of a specified character or substring within a given string.
It returns the index of the first occurrence, or -1 if the character or
substring is not found in the string.
Syntax:
int
indexOf(int ch)
int
indexOf(int ch, int fromIndex)
int
indexOf(String str)
int indexOf(String str, int fromIndex)
Parameters:
Return Value:
Example 1: Using indexOf(char ch) method
public
class StringIndexOfExample {
public static void main(String[] args) {
String str = "Hello, World!";
char ch = 'o';
int index = str.indexOf(ch);
if (index != -1) {
System.out.println("The first
occurrence of '" + ch + "' is at index: " + index);
} else {
System.out.println("'" +
ch + "' not found in the string.");
}
}
}
Output
The
first occurrence of 'o' is at index: 4
Example 2: Using indexOf(String str)
method
public class StringIndexOfExample {
public static void main(String[] args) {
String str = "Java is
awesome!";
String subStr = "is";
int index = str.indexOf(subStr);
if (index != -1) {
System.out.println("The first
occurrence of \"" + subStr + "\" is at index: " +
index);
} else {
System.out.println("\"" + subStr + "\" not
found in the string.");
}
}
}
Output:
The first
occurrence of "is" is at index: 5
In the above examples, we use the indexOf() method to find the index of the first occurrence of a character and a substring in the given string. If the character or substring is found, the method returns the index; otherwise, it returns -1. The indexOf() method is useful for tasks like searching, extracting substrings, and manipulating strings in Java.
The indexOf() method returns the position of the first occurrence of a specified value in a string.
This method returns -1 if the value to search for never occurs.
Note: The indexOf() method is case-sensitive.
Syntax
str.indexOf(str, fromIndex)
Example 1
public class IndexOfExample1 {
public static void main(String[] args) {
String str = "This is an example string";
System.out.println("Index of 's': " + str.indexOf('s'));
System.out.println("Index of 's' from 3rd position: " + str.indexOf('s', 3));
System.out.println("Index of first occurrence of 'is': " + str.indexOf("is"));
System.out.println("Index of last occurrence of 'is': " + str .
The indexOf() method is used to get the integer value of a particular index of a String type object, based on criteria specified by the programmer. This method returns -1 if the value to search for is not present in the string. If the value is present, it returns the position of the first occurrence of that value.
This method can be overloaded by specifying the starting index from which we need to search and also the ending index till which we need to search. We can also specify the character encoding scheme while using this method.
The indexOf() method is used to get the integer value of a particular index of a String type object, based on the criteria specified in the parameters. The first parameter of the indexOf() method is the character or substring that we want to find in the given string. If it is found, then it returns the position of that character or substring within the string. If it is not found, then it returns -1. The second parameter of this method is optional and specifies the starting position from where we want to search for the given character or substring.
The indexOf() method is used to get the integer value of a particular index of a String type object, based on the criteria specified in the parameters. The search for the character or substring begins from the specified start index and ends at the end of the string. If no such character or substring is found then -1 is returned.
For example,
String str = "Hello World!";
int index = str.indexOf('o'); // returns 4
If we consider the above example, the output will be 4 as 'o' is found at index 4 in "Hello World!" string.
The IndexOf() method in Java is used to find the index of a specified character or a substring in a given String. If the specified character or substring is not found, this method returns -1. This method can be overloaded with different signatures. The most common signature of this method is as follows:
int indexOf(String str)
Here, str is the string that has to be searched for within the invoking string object. The return type of this method is an int that represents the index value. If no match is found, -1 is returned.
This method can also be overloaded with a second parameter that denotes the starting index from where the search has to begin:
int indexOf(String str, int fromIndex)
In this case, if a match is found, its index value will be returned; otherwise -1 will be returned. If the fromIndex parameter's value is greater than or equal to the length of the