Java String indexOf() Method
The indexOf() method has the following four forms:
-
public int indexOf(int ch): Returns the index within the string of the first occurrence of the specified character, or -1 if the character is not found.
-
public int indexOf(int ch, int fromIndex): Returns the index within the string of the first occurrence of the specified character, starting the search at the specified index, or -1 if the character is not found.
-
int indexOf(String str): Returns the index within the string of the first occurrence of the specified substring, or -1 if the substring is not found.
-
int indexOf(String str, int fromIndex): Returns the index within the string of the first occurrence of the specified substring, starting the search at the specified index, or -1 if the substring is not found.
Syntax
public int indexOf(int ch)
or
public int indexOf(int ch, int fromIndex)
or
int indexOf(String str)
or
int indexOf(String str, int fromIndex)
Parameters
-
ch -- A character, Unicode code point.
-
fromIndex -- The index to start the search from, where the first character is at index 0, the second is at index 1, and so on.
-
str -- The substring to search for.
Return Value
The index of the first occurrence of the specified character or substring within the string, or -1 if not found.
Example 1
public class Main {
public static void main(String args[]) {
String string = "aaa456ac";
// Find the index of a specified character in the string. If found, return its index; otherwise, return -1.
System.out.println(string.indexOf("b")); // indexOf(String str); returns: -1, "b" not found
// Continue searching from the fourth character position onwards, including the current position
System.out.println(string.indexOf("a", 3)); // indexOf(String str, int fromIndex); returns: 6
// (Difference from previous: previous parameter is String type, this parameter is int type) Reference data: a-97, b-98, c-99
// Search from the beginning for the specified character
System.out.println(string.indexOf(99)); // indexOf(int ch); returns: 7
System.out.println(string.indexOf('c')); // indexOf(int ch); returns: 7
// Search for ch from fromIndex, this is a character variable, not a string. The character 'a' corresponds to the number 97.
System.out.println(string.indexOf(97, 3)); // indexOf(int ch, int fromIndex); returns: 6
System.out.println(string.indexOf('a', 3)); // indexOf(int ch, int fromIndex); returns: 6
}
}
Output:
-1
6
7
7
6
6
The index of the first occurrence of the specified substring within the string, starting from the specified index.
Example 2
public class Test {
public static void main(String args[]) {
String Str = new String("tutorialpro.org:www.tutorialpro.org");
String SubStr1 = new String("tutorialpro");
String SubStr2 = new String("com");
System.out.print("Find the first occurrence of character 'o': ");
System.out.println(Str.indexOf('o'));
}
}
System.out.print("Finding the first occurrence of character 'o' starting from the 14th position: ");
System.out.println(Str.indexOf('o', 14));
System.out.print("The first occurrence of substring SubStr1: ");
System.out.println(Str.indexOf(SubStr1));
System.out.print("The first occurrence of substring SubStr1 starting from the 15th position: ");
System.out.println(Str.indexOf(SubStr1, 15));
System.out.print("The first occurrence of substring SubStr2: ");
System.out.println(Str.indexOf(SubStr2));
The program execution results are:
Finding the first occurrence of character 'o': 12
Finding the first occurrence of character 'o' starting from the 14th position: 17
The first occurrence of substring SubStr1: 9
The first occurrence of substring SubStr1 starting from the 15th position: -1
The first occurrence of substring SubStr2: 16