Java String indexOf()

In this article, we will look at indexOf() method from string class in java, what is it used for, syntax, return value with example programs.

Overview
Java string indexOf() method returns the index of first occurrence of a substring in a string.
Thus, indexOf() can also be used to test if a string contains a sequence of characters or not.
Syntax
indexOf() method accepts a string argument, whose index in the string needs to be found.
Remember that characters in a string start with index 0.

Signature of indexOf() is

public int indexOf(String s)

Return Value
indexOf() returns an integer value which is the index of matching argument string.
If the argument string is not present in the source string, then it returns -1.

Note that indexOf() returns the index of first occurrence of the string argument.
This means that
1. If the supplied string is of 4 characters, indexOf() will return the start index of first character.
Thus, "abc".indexOf("xyzabc") will return 3, the index of “a” in the string.
2. If the argument string occurs multiple times in the source string, then the index of first match will be returned.

Remember that the characters in the source string should be present in the same sequence as the argument.

Java documentation for indexOf() states,

Returns the index within this string of the first occurrence of the specified substring.

String indexOf() example
Below is an example of string indexOf() method

String source = "codippa.com";
String search = ".com";
System.out.println(source.indexOf(search)); // 7

String indexOf() example 2
Below is an example of indexOf(), which contains multiple occurrences of the argument string.

String s = "codippa.com has .com extension";
String s1 = ".com";
System.out.println(s.indexOf(s1)); // 7

Look, indexOf() returns the index of first occurrence of the argument string.
String indexOf() example 3
Below is an example of string indexOf(), where the argument string is not present in the source string.

String s = "codippa.com";
String s1 = "java";
System.out.println(s.indexOf(s1)); // -1

indexOf(), in this case will return -1.
String indexOf() – check substring
String indexOf() method can be used to test if a string exists in another string or not.

Below is an example program, which compares the value returned by indexOf() to check for substring.

String s = "codippa.com";
String s1 = ".com";
if(s.indexOf(s1) != -1) {
  System.out.println("Substring exists");
} else {
  System.out.println("Substring does not exist");
}

This prints

Substring exists

You can also compare the value returned by indexOf() >= 0 to check for the existence of substring.
String contains() method also internally does the same.

String indexOf() case sensitive
indexOf() performs case sensitive search.
This means that the string to be checked should exist in the same case in source string, otherwise indexOf() will not be able to find it and will return -1. Example,

String source = "codippa.com"; 
String search = ".Com"; 
System.out.println(source.indexOf(search)); // -1

Thus, the comparison by indexOf() is not insensitive.

Hope the article on Java string indexOf() method was useful.