Understanding the Java String IndexOf Method
String handling and string manipulation is one of the most common tasks that need to be performed in any software application. Different languages implement strings in different ways. For instance, in the C language, strings are implemented in the form of a character type array. In the more advanced languages, based on object oriented principles, Strings are implemented in the form of objects of standalone string classes. This is the case with Java as well, where strings are implemented in the form of String classes. Implementing strings in the form of classes has several advantages since it allows programmers to associate methods with the String class which can later be used to manipulate string objects. One such method is the Java String IndexOf method.
What is Java String IndexOf Method?
The Java string indexOf method is a function that is used to get the integer value of a particular index of String type object, based on a criteria specified in the parameters of the IndexOf method. A typical scenario can be when a system administrator wants to find the index of the ‘@’ character of the email Id of a customer and then wants to get the remaining substring. In such cases, the IndexOf method can be used.
How Java String IndexOf Method works?
The Java String IndexOf method has four overloads. All the overloads return an integer type value, representing the returned index. These overloads differ in the type and number of parameters they accept. These overload methods have been described below.
- IndexOf(char c)
This method returns the index of the character ‘c’ passed as parameter. If the specified character is not present in the string, the returned index would be -1.
- IndexOf(char c, int fromindex)
The above method would return the index of the first occurrence of character ‘c’ after the integer index passed as second parameter “fromindex”. All the occurrences of character ‘c’ before the “fromindex” integer index would be ignored.
- IndexOf(String substring)
This method returns the index of the first character of the substring passed as parameter to it. If the specified substring is not present in the string, the returned index would be -1.
- IndexOf(String substring, int fromindex)
This method returns the index of the first character in the substring passed as the first parameter, after the fromindex, index value. If substring starts from the passed integer value of fromindex, that substring would be ignored.
Working Examples of Java String IndexOf Method
The four examples in this section explain the four overloads of the java string indexOf method.
The first example demonstrates a simple Java String IndexOf method that takes only one character type parameter:
String str = new String ("Udemy Java Tutorials"); int indexOfJ = str.indexOf('J'); int indexOfj = str.indexOf('j'); int indexOfq = str.indexOf('q'); System.out.println(indexOfJ); System.out.println(indexOfj); System.out.println(indexOfq);
In the above example, a String type object str has been declared. This String object contains a literal value of “Udemy Java Tutorials”.
Next, three integer type variables, namely indexOfJ, indexOfj and indexOfq has been declared which would contain the values returned by indexOf(‘J’), indexOf(‘j’), indexOf(‘q’) respectively.
The first Java String indexOf method has been called and a character ‘J’ has been passed to it. This method will return index of J in the string str which would be 6. It is worth noting here that string based index start from zero. It means that the first character has 0index and last character has the index of length of string -1.
In the next line, again indexOf method has been called but this time a small letter ‘j’ has been passed to it. But since string doesn’t contain a small letter ‘j’, therefore -1 will be returned. This shows that indexOf method is case sensitive.
Finally, if a character is not present in a string, the indexOf method would return -1 as in the third case of the above example where ‘q’ has been passed to the indexOf method which is not present in the String type object str. The output of Example1 would be:
This example demonstrates the indexOf method overload which takes a character type first element and the integer type fromindex as the second element:
String str = new String ("Udemy Java Tutorials"); int indexOfaAfter10 = str.indexOf('a',10); System.out.println(indexOfaAfter10);
In the aboe example, the indexOf method would return that index of character ‘a’ which occurs in the string after the 10th index which would be 17. In the String type object, though there are two letters ‘a’ in the word Java but they would be ignored and the index of the letter ‘a’ in the word Tutorials would be returned since it occurs after 10th index. If no such character exits after the specified index, -1 would be returned.
The following example demonstrates Java String indexOf method overload that takes one string type variable as parameter and returns the starting index of that string:
String str = new String ("Udemy Java Tutorials"); int indexOfJava = str.indexOf("Java"); System.out.println(indexOfJava);
In the above example, the indexOf method receives a string literal “Java” as parameter. The index method would look for this string in the str string and would return the starting index of that string which would be 6 in the above case.
The following example demonstrates Java String indexOf method overload that takes one string type variable as parameter and the other integer type indexfrom variable and returns the starting index of that string after the specified index:
String str = new String ("Udemy Java Tutorials for Java Beginners"); int indexOfJava = str.indexOf("Java", 10); System.out.println(indexOfJava);
In the above example, there are two substrings “Java” in the main string str. The indexOf method takes two parameters: A literal string “Java” whose starting index has to be found and integer type fromindex 10, which specifies the index after which the “Java” string’s starting index has to be returned which would be 25 in this case.
Top courses in Java
Java students also learn
Empower your team. Lead the industry.
Get a subscription to a library of online courses and digital learning tools for your organization with Udemy Business.