indexOf() method is a way of finding the index of the first occurrence of a character or a string. In this post, we will discuss the indexOf() method of the String class in detail.
indexOf() method has below overloaded methods –
- public int indexOf(int ch)
- public int indexOf(int ch, int fromIndex)
- public int indexOf(String str)
- public int indexOf(String str, int fromIndex)
public int indexOf(int ch)
- What does it do? It will find the index of the first occurrence of the specified character. The first occurrence means that if there are multiple occurrences of the character in the string, it will return the index of the first occurrence only.
- What does it return? It will return the index of the first occurrence of the character if found in the string. Otherwise, if the character is not found in the string, it would return -1.
Remember, the indexing will start from 0. So, if the character is present at the 3rd position in the string, then the method will return 2 to the calling function.
Code example
public class Codekru {
public static void main(String[] args) {
String str1 = "codekru hii! codekru";
System.out.println("index of character 'i' is: " + str1.indexOf('i'));
System.out.println("index of character 'm' is: " + str1.indexOf('m')); // this will give -1, as 'm' is not present in the string
}
}
Output –
index of character 'i' is: 9
index of character 'm' is: -1
- As there were two i’s in the string, indexOf() returned the index of the first ‘i’ present in the string.
- And it returned -1 for the character ‘m’, which was not present in the string at all.
Time complexity of indexOf(int ch) method
The time complexity of the indexOf(int ch) method is O(n) where n is the size of the string being iterated.
- The best case time complexity of the string is O(1), when the character is found at the first position.
- And the worst-case time complexity is O(n) when the character is found at the last position or not found at all.
public int indexOf(int ch, int fromIndex)
- What does it do? It will find the index of the first occurrence starting from the index passed into the function’s argument. So, if the fromIndex argument is given as 6, then the function will begin finding the character from index 6 ( 0 -based indexing).
- What does it return? It also returns the index if a character was found starting from a particular index. Otherwise, it will return -1.
Code example
public class Codekru {
public static void main(String[] args) {
String str1 = "codekru hii! codekru";
System.out.println("index of character 'c' is: " + str1.indexOf('c', 9));
System.out.println("index of character 'i' is: " + str1.indexOf('i', 13)); // this will give -1, as 'i' is not present in the string after index 13
}
}
Output –
index of character 'c' is: 13
index of character 'i' is: -1
In this, the index of character ‘c’ was returned as 13, though ‘c’ was present at the index 0( zero ) too. This was because we passed the starting index as 9 in the function’s argument, so it will look for ‘c’ starting from the index 9 instead of index 0( zero).
Did you know? indexOf(int ch) internally uses indexOf(int ch, int fromIndex) with the fromIndex value as 0
public int indexOf(int ch) {
return indexOf(ch, 0);
}
Time complexity of indexOf(int ch, int fromIndex) method
The time complexity of indexOf(int ch, int fromIndex) method is also O(n).
public int indexOf(String str)
- What does it do? It finds the first occurrence of the string passed into the function.
- What does it return? It returns an int representing the index of the first occurrence of the string. It will return -1, if the string is not found.
Code example
public class Codekru {
public static void main(String[] args) {
String str = "hi codekru!! hi codekru";
String s1 = "codekru";
String s2 = "hello";
System.out.println("first occurrence of 'codekru' is at index: " + str.indexOf(s1));
System.out.println("first occurrence of 'hello' is at index: " + str.indexOf(s2));
}
}
Output –
first occurrence of 'codekru' is at index: 3
first occurrence of 'hello' is at index: -1
There were two occurrences of the string “codekru” present at the different indices, but indexOf() returned the index of the first occurrence of the string( Remember the indexing is 0-based ). It returned -1 for the string “hello” which was not present in the string.
Time complexity of indexOf(String str) method
The time complexity of the indexOf(String str) method is O(m*n) where m is the length of the string in which we have to search, and n is the length of the string passed in the arguments.
public int indexOf(String str, int fromIndex)
- What does it do? It finds the first occurrence of the string passed in the function starting from the index passed as the second argument.
- What does it return? It returns an integer value representing the index of the specified string starting from a particular index. If the string is not found, it will return -1.
Code example
public class Codekru {
public static void main(String[] args) {
String str = "hi codekru!! hi codekru";
String s1 = "codekru";
String s2 = "hello";
System.out.println("first occurrence of 'codekru' is at index: " + str.indexOf(s1, 7));
System.out.println("first occurrence of 'hello' is at index: " + str.indexOf(s2, 8));
}
}
Output –
first occurrence of 'codekru' is at index: 16
first occurrence of 'hello' is at index: -1
Here the searching for the specified string starts from index 7, and the string was found at index 16. Hence the indexOf() method returned 16.
Time complexity of indexOf(String str, int fromIndex) method
The time complexity of the indexOf(String str, int fromIndex) method is O(m*n) where m is the length of the string in which we have to search, and n is the length of the string passed in the arguments.
The What If scenarios
Q – What if we passed an index < 0 in the indexOf() method?
No, it won’t throw you an exception. If we pass a negative index, then 0 (zero) will be considered the starting point for searching the string or the character.
public class Codekru {
public static void main(String[] args) {
String str = "hi codekru!! hi codekru";
String s1 = "codekru";
System.out.println("first occurrence of 'codekru' is at index: " + str.indexOf(s1, -120));
}
}
Output –
first occurrence of 'codekru' is at index: 3
Q- What if we passed an index >= string length in the indexOf() method?
In this scenario, it will return -1 as shown in the below code.
public class Codekru {
public static void main(String[] args) {
String str = "hi codekru!! hi codekru";
String s1 = "codekru";
System.out.println("first occurrence of 'codekru' is at index: " + str.indexOf(s1, str.length()));
}
}
Output –
first occurrence of 'codekru' is at index: -1
Q- What if we use the indexOf() method on a null string?
Here, we will get a NullPointerException as illustrated by the below example.
public class Codekru {
public static void main(String[] args) {
String str = null;
System.out.println("index of character 'i' is: " + str.indexOf('i'));
}
}
Output –
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "String.indexOf(int)" because "str" is null
If you want to learn more about the String and its methods, we suggest you read this article.
We hope that you have liked the article. If you have any doubts or concerns, please feel free to write us in the comments or mail us at admin@codekru.com.