Java String indexOf()

The indexOf method is used to get the integer value of a particular index of String type object, based on criteria specified in the parameters of the IndexOf method.

A common scenario can be when a system admin wants to find the index of the '@' character of the email Id of a client and then wants to get the remaining substring. In that situation, IndexOf method can be used.

The java string indexOf() method returns index of given character value or substring. If it is not found, it returns -1. The index counter starts from zero.

Internal implementation
public int indexOf(int ch) { return indexOf(ch, 0); }
Syntax
public int indexOf(int cha)
Parameters

cha − a character.

Return Value

This Java method returns the index within this string of the first occurrence of the specified character. It returns -1 if the character does not occur.

The Java String IndexOf method has four overloads. All the overloads return an integer type value, representing the returned index. These overloads differ in the type and number of parameters they accept.

IndexOf(char b)

This method returns the index of the character 'b' passed as parameter. If that character is not available in the string, the returned index would be -1.

IndexOf(char c, int startindex)

The given method would return the index of the first occurrence of character 'c' after the integer index passed as second parameter "startindex." All the occurrences of character 'c' before the "startindex" integer index would be ignored.

IndexOf(String substring)

The above method returns the index of the first character of the substring passed as a parameter to it. If that substring is not available in the string, the returned index would be -1.

IndexOf(String substring, int startindex)

This Java method returns the index of the first character in the substring passed as the first parameter, after the "startindex" index value. If substring starts from the passed integer value of "startindex", that substring would be ignored.

Java String indexOf() method example
public class Test1 { public static void main(String args[]) { String s1="this is cprogramcoding of example"; //passing substring int in1=s1.indexOf("is"); int in2=s1.indexOf("cprogramcoding"); System.out.println(in1+" "+in2); //passing substring with from cprogramcoding int in3=s1.indexOf("is",4); System.out.println(in3); //passing char value int in4=s1.indexOf('s'); System.out.println(in4); } }


Output:
2 8 5 3
Java String indexOf() method example
public class Test1 { public static void main(String args[]) { String str_Sample = "This is Index of Example"; //Character at position System.out.println("Index of character 'x': " + str_Sample.indexOf('x')); //Character at position after given index value System.out.println("Index of character 's' after 3 index: " + str_Sample.indexOf('s', 3)); //Give index position for the given substring System.out.println("Index of substring 'is': " + str_Sample.indexOf("is")); //Give index position for the given substring and start index System.out.println("Index of substring 'is' form index:" + str_Sample.indexOf("is", 5)); } }


Output:
Index of character 'x': 12 Index of character 's' after 3 index: 3 Index of substring 'is': 2 Index of substring 'is' form index:5
Java String indexOf(String substring) Method Example

This method takes substring as an argument and returns index of first character of the substring.

public class Test1 { public static void main(String[] args) { String s1 = "This is indexOf method"; // Passing Substring int index = s1.indexOf("method"); //Returns the index of this substring System.out.println("index of substring "+index); } }


Output:
index of substring 16
Java String indexOf(String substring, int fromIndex) Method Example

This method takes substring and index as arguments and returns index of first character occured after the given fromIndex.

public class Test1 { public static void main(String[] args) { String s1 = "This is indexOf method"; // Passing substring and index int index = s1.indexOf("method", 12); //Returns the index of this substring System.out.println("index of substring "+index); index = s1.indexOf("method", 25); System.out.println("index of substring "+index); } }


Output:
index of substring 16 index of substring -1
Java String indexOf(int char, int fromIndex) Method Example

This method takes char and index as arguments and returns index of first character occured after the given fromIndex.

public class Test1 { public static void main(String[] args) { String s1 = "This is indexOf method"; // Passing char and index from int index = s1.indexOf('o', 12); //Returns the index of this char System.out.println("index of char "+index); } }


Output:
index of char 20



Instagram