indexOf(String): Get the index of a String in another String

What is indexOf(String)?


It is a method of the java.lang.String class that gets the starting index of a string in another string. It is an overloaded method. Actually there are four methods, but here we will talk about only two methods, the methods that take String as one of its parameter.

  public int indexOf(java.lang.String);
  public int indexOf(java.lang.String, int);

indexOf(String): This method takes java.lang.String as parameter and returns the starting index of string. The string in which the given string (the parameter) is searched is detected by the String class and is nothing but the string on which it was called. For clear understanding the concept, let us consider two strings.

The main string, take "I am the main string." for example.
The string to be searched with in the main string, let us consider "the main".

If we want to get the index of "the main" in "I am the main string.", then you will have to call the indexOf(String) method on the "I am the main string." and then pass "the main" as parameter. Then it returns an int value which contains, 5 (here).

indexOf(String, int): This is much similar than the above, but the next parameter int is the index from which the searching has to start. In the above method, it is 0

For example, let us consider the main string "Repeat Repeat my boy!" and the search string "Repeat". When i pass 2 (or something greater than 0), then the index of the second Repeat will be returned i.e. 7 (here).


What if search string is not found?


Simple. The method returns -1. Then, a possible question may also arise in your mind, why only -1, this is because, usually there is no index like -1 because index of an array in programming start from 0 as we know, therefore any other number greater than 0 cannot be eligible for returning because if they return 10, then it means that the search string is at 11th position (when counted first char as 1 else when counted first char as 0, the index then 10th position), in fact this was not true because the string was not found. Hope you understand, if you don't feel free to drop a comment.


The Example


class indexOfString
{
public static void main(String args[])
{
System.out.println("I am the main string.".indexOf("the main"));
System.out.println("Repeat Repeat my boy!.".indexOf("Repeat",4));
}
}

-------------------------------------
Output
-------------------------------------

5
7

What is this? How can you do "".indexOf("")?


We can do it. Yes, as we know that anything that is kept in double quotes " " is a String object. So that is why i have done it. If you don't like it, ok then, do it in your way ;)


What still i have to note?


You will have to note that, the indexOf(String) and the indexOf(String, int) both the methods does not ignore case i.e. they are case sensitive. For example, instead of "Repeat" in the above example, if you send "repeat" as parameter, then the method returns -1 which means that the String is not found.


Posted by at on

Tags: java.lang, String Handling,