IndexOf In Java Example
Chapter:
String Handling
Last Updated:
09-06-2017 13:11:14 UTC
Program:
/* ............... START ............... */
public class JavaIndexOf {
public static void main(String args[]) {
String s1 = "this is index of example";
// passing substring
int index1 = s1.indexOf("is");// returns the index of is substring
int index2 = s1.indexOf("index");// returns the index of index substring
System.out.println(index1 + " " + index2);
}
}
/* ............... END ............... */
Output
Notes:
-
The java string indexOf() method returns index of given character value or substring.
- If index is not found, it returns -1. The index counter starts from zero.
- There are four types of indexOf method in java.
- int indexOf(int ch): It returns the index of the first occurrence of character ch in a String.
- int indexOf(int ch, int fromIndex): It returns the index of first occurrence if character ch, starting from the specified index “fromIndex”.
- int indexOf(String str): Returns the index of string str in a particular String.
- int indexOf(String str, int fromIndex): Returns the index of string str, starting from the specified index “fromIndex”.
Tags
IndexOf, Java