public class IndexOfExample
{
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); // 2 8
// Passing substring with from index
int index3 = s1.indexOf("is", 4); // returns the index of "is" substring after 4th index
System.out.println(index3); // 5 i.e. the index of another "is"
// Passing char value
int index4 = s1.indexOf('s'); // returns the index of 's' char value
System.out.println(index4); // 3
}
}
Step-by-Step Explanation
1. Class and Method Declaration:
public class IndexOfExample
{
public static void main(String args[])
{
- In the program, a class named IndexOfExample is defined.
- The main method is the program entry point. Here, execution starts.
2. String Declaration:
String s1 = “this is index of example”;
A string variable s1 is declared and initialized with value “this is index of example”.
3. First indexOf() Invocation (Substring):
int index1 = s1.indexOf(“is”);
- indexOf(“is”) invokes the indexOf method on string s1 to determine where in string s1 the first occurrence of substring “is” appears.
- s1 = “this is index of example”; and substring “is” first occurs beginning at index 2 – which is the third character in string s1.
- The indexOf() method returns the index of the first occurrence of the specified substring, which in this case is 2.
- Value of index1: 2
4. Second indexOf() Call (Another Substring):
int index2 = s1.indexOf(“index”);
- The call of method indexOf(“index”) is used to find the first occurrence of the substring “index” within the string s1.
- The string s1 = “this is index of example”, and the substring “index” is seen for the first time from index 8
- Value of index2: 8
5. Print the First Two Indexes:
System.out.println(index1 + ” ” + index2);
The statement above will print the value of index1 and index2
Output will be :
index1 is 2.
index2 is 8.
Output: 2 8
6. Third indexOf() Call (Substring with Starting Index):
int index3 = s1.indexOf(“is”, 4);
- The indexOf(“is”, 4) method call is looking for the substring “is”, but it starts searching from index 4 forward, that is, after the 4th index.
- The string s1 = “this is index of example”
- The first “is” is at index 2, but we start searching from index 4.
- The second “is” starts from index 5.
- Value of index3: 5
7. Print the Third Index:
System.out.println(index3);
- This statement prints the value of index3.
- The value of index3 is 5.
- Output: 5
8. Fourth indexOf() Call (Character):
int index4 = s1.indexOf(‘s’);
- The indexOf(‘s’) method call is searching for the first index of the character ‘s’ in the string s1.
- The string s1 = “this is index of example”, and the character ‘s’ first appears at index 3.
- Value of index4: 3
9. Printing the Fourth Index:
System.out.println(index4);
- This statement prints the value of index4.
- The value of index 4 is 3.
- Output: 3
Program Output:
2 8
5
3
Summary of Key Points:
1. indexOf(“is”) returns the index of the first occurrence of the substring “is”, that is 2.
2. indexOf(“index”) returns the index of the first occurrence of the substring “index, that is 8.
3. indexOf(“is”, 4) returns the index of the next occurrence of “is” after index 4, that is 5.
4. indexOf(‘s’) returns the index of the first occurrence of the characters, that is 3.
Method Detail:
- indexOf(String substring): Find the index of the first occurrence of the specified substring within this string.
- indexOf(String substring, int fromIndex): Finds the first occurrence of the specified.
- substring after the index. indexOf(char ch): Finds the index of the first occurrence of the specified character within the string.