ZMedia Purwodadi

Java String indexOf() method explained | technical-arbaab

Table of Contents

Java String indexOf() method 


It is a predefined function which is present in the Java.lang.String package. It is used to find the index of specific character in your string. It will return integer type. It is of four types based upon the parameter it will take, let us discuss each of them in detail.

Types of indexOf() methods in Java

  public int indexOf(String str) {}  
  public int indexOf(int ch) {}  
  public int indexOf(String str, int fromIndex) {}  
  public int indexOf(int ch, int fromIndex) {}  

  • public int indexOf(String str) {}:

This method takes the string as a parameter and checks only the very first occurrence of string passed in this method for example-

Example 01 of indexOf(String str) method

 public class IndexOf {  
      public static void main(String[] args)  
      {  
           String s = new String("Nature");  
           System.out.println(s.indexOf("t"));  
      }  
 }  
Output:
 2  

here, what actually happened is 't' is at index 2 of the string "Nature" that is why it will return 2 and hence this method is called in the print line stream, it has also printed on the console.

Example 02 of indexOf(String str) method

 public class IndexOf {  
      public static void main(String[] args)  
      {  
           String s = new String("bugaati veyron is so awesome");  
           System.out.println(s.indexOf("veyron"));  
      }  
 }  
Output:
 8  
here, we have written indexOf("veyron") which means to return the index of the very first occurrence of "veyron" in the whole string and we know that the "veyron" is started from index of v which is at index number 8 and hence it has returned 8 and is also printed on the console.
  • public int indexOf(int ch) {}:

Example 01 of indexOf(int ch) method

 public class IndexOf {  
      public static void main(String[] args)  
      {  
           String s = new String("black");  
           System.out.println(s.indexOf(97));  
      }  
 }  
Output:
 2  
This method searches the character from the ASCII value of it, for example here in the above example we want to know the index of element whose ASCII value is 97, user have to give this value(ASCII value) and we very well know that 97 is the ASCII value of 'a' (if you want to know how to get the ASCII value of characters through java program refer to: ASCII value of characters using Java program) so it will search where is the character 'a' and simply returns it's index which you can output on the console.
  • public int indexOf(String str, int fromIndex) {}:

Example 01 of indexOf(String str, int fromIndex) method

 public class IndexOf {  
      public static void main(String[] args)  
      {  
           String s = new String("black is always black");  
           System.out.println(s.indexOf("l",9));  
      }  
 } 
Output:
 10  

This method indexOf(String str, int fromIndex) actually gives you the index of the string passed but there is one more parameter which is nothing but to give the index from where you want to start the search of the string whose index you want to return, for example in the above example we have written indexOf("l",9) which means to return the index of very first occurrence of 'l' in the string and start searching from the index number 9 where 'a' is there so from the character 'a' searching will be start and at the very next index 'l' is there at index number 10 hence it has returned 10. 

Example 02 of indexOf(String str, int fromIndex) method

 public class IndexOf {  
      public static void main(String[] args)  
      {  
           String s = new String("black is always black and always black");  
           System.out.println(s.indexOf("always",15));  
      }  
 }  
Output:
 26  

here indexOf("always",15) will search for string "always" from the index number 15 and hence the index of second "always" in the string is returned.

  • public int indexOf(int ch, int fromIndex) {}:

Example 01 of indexOf(int ch, int fromIndexmethod

 public class IndexOf {  
      public static void main(String[] args)  
      {  
           String s = new String("Hustle is Hustle");  
           System.out.println(s.indexOf(72,1));  
      }  
 }  
Output:
 10  

In this example, we write indexOf(72,1) which means to search the element whose ASCII value is 72 ( we know that the ASCII value of 'H' is 72) and start the search from the index number 1 so as we move further we find that another 'H' is come at index number 10 and hence it returned 10.

Example 02 of indexOf(int ch, int fromIndexmethod

 public class IndexOf {  
      public static void main(String[] args)  
      {  
           String s = new String("elephant");  
           System.out.println(s.indexOf(101,1));  
      }  
 }  
Output:
 2  

In this example, we write indexOf(101,1) which means to search the element whose ASCII value is 101 (we know that 'e' has the ASCII value 101) and start the search from index number 1 where character 'l' is there so as we move further we find that at the index number 2 'e' (ASCII value is 101) is there so it has returned 2.

If you find this article helpful let us know in the comment section below and share it with your friends who are java lovers.

Arbaab khan
Arbaab khan FEAR has two meanings "forget everything and run" "Face everything and rise" The choice is yours Welcome u in our tech blog

إرسال تعليق