In this tutorial, you will learn about String indexOf() method, its syntax, and usage with examples.
Java String indexOf() method
In Java, String indexOf() method is used to find the index of first occurrence of given value in the string.
Syntax of indexOf()
There are four indexOf() methods defined in String class. Based on the type of argument, and number of arguments passed, respective method is called.
public int indexOf(String str)
public int indexOf(String str, int fromIndex)
public int indexOf(int char)
public int indexOf(int char, int fromIndex)
Let us summarise all these four definitions, and specify a single syntax to call String indexOf() method.
string.indexOf(value, fromIndex)
where
Parameter | Description |
---|---|
value | [Mandatory] A String or an int type value. (If int is given, then the integer represents a character value like ‘a’, ‘b’, ‘A’, ‘5’, etc.)The value which is searched in the string to find the index of. |
fromIndex | [Optional] An int type value.The position in the string from which search for value should happen. |
indexOf() returns value of int type.
If the given value is present in the string, then indexOf() method returns the index of the value in the string.
If the given value is not present, then indexOf() method returns -1
.
The following are some of the examples to call indexOf() on str string with valid arguments.
// indexOf(String str)
str.indexOf("World")
// indexOf(String str, int fromIndex)
str.indexOf("World", 20)
// indexOf(int char)
str.indexOf(65)
str.indexOf('A')
// indexOf(int char, int fromIndex)
str.indexOf(65, 4)
str.indexOf('A', 4)
Examples
1. indexOf() – Find the index of value “World” in the string in Java
In this example, we take two strings: str and value; and find the index of the value in the string str using String indexOf() method.
Java Program
public class Main {
public static void main(String[] args) {
String str = "Hello World";
int index = str.indexOf("World");
System.out.println("Index : " + index);
}
}
Output
Index : 6
Explanation
H e l l o W o r l d ← given string
↑
W o r l d ← value to search for in the string
0 1 2 3 4 5 6 7 8 9 10 ← indices of characters in the string
↑
6 is where the value is found for first time in the string
6 is the return value
2. indexOf() – Find the index of value “World” in the string from a starting index of 10 in Java
In this example, we take two strings: str and value; and find the index of the value in the string str from a starting index of 10 using String indexOf() method. The value is searched in the string from index=10. Even if there is an occurrence of the value in the string before the given starting index, that occurrence is ignored.
Java Program
public class Main {
public static void main(String[] args) {
String str = "Hello World. Welcome to new World";
int index = str.indexOf("World", 10);
System.out.println("Index : " + index);
}
}
Output
Index : 28
Explanation
"Hello World. Welcome to new World" ← given string
012.. 10
↑
fromIndex
↑
World ← value to search for in the string
28 is index of value in string
3. indexOf() – When the search value is not present in the string
In this example, we take two strings: str and value; such that the value is not present in the string str, and observe the return value of indexOf() method.
Java Program
public class Main {
public static void main(String[] args) {
String str = "Hello World";
int index = str.indexOf("Apple");
System.out.println("Index : " + index);
}
}
Output
Index : -1
Since the given search value "Apple"
is not present in the string "Hello World"
, the indexOf() method returns -1.
4. indexOf() – Find the index of character ‘W’ in the string in Java
In this example, we will find the index of the character 'W'
in the string str using String indexOf() method.
You may wonder that how could we give a character value ‘W’ as argument to indexOf() method, when it takes only a string or int value. Please notice that the char value can be implicitly converted to respective integer value.
Let us go ahead and find the index of our 'W'
in the string.
Java Program
public class Main {
public static void main(String[] args) {
String str = "Hello World.";
int index = str.indexOf("W");
System.out.println("Index : " + index);
}
}
Output
Index : 6
Explanation
H e l l o W o r l d ← given string
↑
W ← value to search for in the string
0 1 2 3 4 5 6 7 8 9 10 ← indices of characters in the string
↑
6 is where the value is found for first time in the string
6 is the return value
Conclusion
In this Java String Methods tutorial, we have seen about String indexOf() method in Java, its syntax, and how to use String indexOf() method in Java programs with the help of examples.