Hello all👋 I hope you are doing well. Again this is going to be a very short and very useful article. In this article I'll be writing about indexOf() method in Java. This is a one of the most useful methods.
Let's begin...
indexOf()
This method lets you find a string within another string. The indexOf() method searches for the first occurrence of a character or substring. This method returns the index position of the first occurrence of a specified string. This is a method of Java String class. In other words, this method retrieves the index value associated with a particular character or substring in a string. If the character or phrase does not occur in the string, indexOf() returns -1.
Syntax:-
stringName.indexOf(char ch);
There are different variations in which indexOf() can be used:-
1. indexOf(char ch)
In this variation we have printed the index value of first occurrence of character i
public class Demo {
public static void main(String[] args) {
String str = "We will find index of i in this";
System.out.println("Index of the character i is : " + str.indexOf('i'));
}
}
You can run your code online here
2. indexOf(char ch, int start)
In this variation we have printed the index value of character i but not at its first occurrence, the character i first occurs at index 4, so we gave it starting value greater than 4
public class Demo {
public static void main(String[] args) {
String str = "We will find index of i in this";
System.out.println("Index of the character i is : " + str.indexOf('i', 5));
}
}
You can run your code online here
3. indexOf(String str)
In this variation we have printed the index value of first occurrence of string in
public class Demo {
public static void main(String[] args) {
String str = "We will find index of in here";
System.out.println("Index of the String in is : " + str.indexOf("in"));
}
}
You can run your code online here
4. indexOf(String str, int start)
In this variation we have printed the index value of String in but not at its first occurrence, the String in first occurs at index 9, so we gave it starting value greater than 9
public class Demo {
public static void main(String[] args) {
String str = "We will find index of in here";
System.out.println("Index of the String in is : " + str.indexOf("in", 10));
}
}
You can run your code online here
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.
Top comments (2)
Great Explanation about Java indexOf() Method but I have one question why you are prefering run java program on one source. If you search on google their are lots of better online java compiler for example this online compiler I personally run the java program there and would like to suggest you should also try it.
This data science course in Mumbai stands out for its comprehensive curriculum and experienced faculty. If you're serious about a career in data science, look no further!