Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
A sequence of characters is known as a String. It is also an object whose value is enclosed within double quotes. Accessing and manipulating the characters are the two most common operations carried out on strings. These operations can be performed by using the functions/methods of the String class. One such function is the indexOf() method used to find the index of the first occurrence of a substring/character in the string.
Java String Indexing
In Java, every character in a String has a particular index associated with it. Similar to arrays, the first character is of index 0, followed by the next character with index 1. Thus the last character of the String will have an index that is one less than the length of the entire String.
For example, if the string is “hello world”, the index of ‘h’ is 0, and the index of ‘d’ is 10.
Signature
Method Signature
Description
int indexOf(int ch)
Returns the index of the first occurrence of the specified character in the string, or -1 if the character is not found.
int indexOf(int ch, int fromIndex)
Returns the index of the first occurrence of the specified character in the string, starting the search at the specified index, or -1 if the character is not found.
int indexOf(String str)
Returns the index of the first occurrence of the specified substring in the string, or -1 if the substring is not found.
int indexOf(String str, int fromIndex)
Returns the index of the first occurrence of the specified substring in the string, starting the search at the specified index, or -1 if the substring is not found.
The indexOf() function
The indexOf() helps find the location of the first occurrence of a character/substring in a string. It takes the substring to be located as its parameter and returns a positive integer value that gives the location in terms of an index. If the specified substring is not present in the String, it returns -1.
Using the indexOf() function can help determine the type of character at a position and count the number of its occurrences, if any.
The indexOf() function can be used in four ways as it is an overloaded function. Let's learn each of them in detail with examples.
Variants of IndesOf() method
The `indexOf()` method in Java is essential for string manipulation, allowing you to find the position of characters or substrings within a string. Let's see its variants:
1. indexOf(int ch) - This method returns the index of the first occurrence of the specified character within the string.
2. indexOf(int ch, int fromIndex) - It returns the index of the first occurrence of the specified character, starting the search at the specified index.
3. indexOf(String str) - This method returns the index of the first occurrence of the specified substring.
4. indexOf(String str, int fromIndex) - It returns the index of the first occurrence of the specified substring, beginning at the specified index.
These variants provide flexibility in searching for characters or substrings within a string, which makes `indexOf()` a powerful tool for Java developers.
Overloaded indexOf() methods
indexOf(int ch)
Syntax:
int indexOf(char ch)
You can also try this code with Online Java Compiler
It returns the index of the first occurrence of a character within the string’s limits. If not found, the function returns -1.
class Main
{
public static void main(String[] args)
{
String s = "hello world";
int length = s.length();
System.out.println("Length of the string is: " + length);
int result;
result = s.indexOf('h');
System.out.println("index of h: " + result);
result = s.indexOf('l');
System.out.println("index of l(first occurance): " + result);
result = s.indexOf('j');
System.out.println("index of j:" + result);
result = s.indexOf("");
System.out.println("index of empty string: " + result);
}
}
You can also try this code with Online Java Compiler
It returns the index of the first occurrence of a character starting from the given index in the string. If not found, the function returns -1.
class Main
{
public static void main(String[] args)
{
String s = "hello world";
int result;
result = s.indexOf('h',4);
System.out.println("index of h from index 4: " + result);
result = s.indexOf('l',4);
System.out.println("index of l(first occurance) from index 4: " + result);
}
}
You can also try this code with Online Java Compiler
It returns the index of the first occurrence of a substring within the string’s limits. If not found, the function returns -1.
class Main
{
public static void main(String[] args)
{
String s = "hello world";
int result;
result = s.indexOf("llo");
System.out.println("index of substring llo: " + result);
result = s.indexOf("o w");
System.out.println("index of substring o w: " + result);
result = s.indexOf("helo");
System.out.println("index of substring helo: " + result);
}
}
You can also try this code with Online Java Compiler
It returns the index of the first occurrence of a substring starting from the given index in the string. If not found, the function returns -1.
class Main
{
public static void main(String[] args)
{
String s = "hello world";
int result;
result = s.indexOf("llo",4);
System.out.println("index of substring llo from index 4: " + result);
result = s.indexOf("rld",4);
System.out.println("index of substring rld from index 4: " + result);
result = s.indexOf("o w",4);
System.out.println("index of substring o w from index 4: " + result);
result = s.indexOf("helo",4);
System.out.println("index of substring helo from index 4: " + result);
}
}
You can also try this code with Online Java Compiler
index of substring llo from index 4: -1
index of substring rld from index 4: 8
index of substring o w from index 4: 4
index of substring helo from index 4: -1
How to find all occurrences of a character or substring in Strings in Java?
The last occurrence of a substring /character can be found using the lastIndexOf() function. To find all the occurrences, we can use the indexOf() function in a loop and maintain a counter. On discovering the first occurrence, replace it with whitespace and increment the counter.
What is the main difference between Strings in Java, C and C++?
In C and C++, strings are terminated with null characters and are stored as character arrays. In Java, strings are not terminated with null characters. They are treated as objects.
What is the difference between the Charat () and indexOf () methods?
CharAt() retrieves a character at a specific index within a string, while indexOf() finds the position of a character or substring within a string.
Conclusion
In this article, we have discussed the `indexOf()` function in Java in detail. We began with a discussion on indexing within Java strings and then looked into the various overloaded versions of the `indexOf()` function, with proper examples to show its use.