String indexOf(int ch, int fromIndex)

In this section, you will get the detailed explanation about the indexOf(int ch, int fromIndex) method of String class.

String indexOf(int ch, int fromIndex)

String indexOf(int ch, int fromIndex)

     

In this section, you will get the detailed explanation about the indexOf(int ch, int fromIndex) method of String class. We are going to use indexOf(int ch, int fromIndex) method of String class in Java. The description of the code is given below for the usage of the method.

Description of the code:

Here, you will get to know about the
indexOf(int ch, int fromIndex) method through the following java program. In the program code given below, we have taken a String. To find the index of any character in a string we have applied indexOf(int ch, int fromIndex); method and then we have passed the character from a string in that with the index number to find its index starting from the specified index as shown in the output. You must be observing in the output that we have also taken the ASCII value of small "b" which is 98 with the index number. Hence its also giving the same result.

NOTE: This method returns the index of the first occurrence of the given character, starting the search at the specified index within the string.

Here is the code of the program:

public class IndexOfChar{
  public static void main(String args[]) {
  String s = "Honesty is the best Policy";
  System.out.println(s);
  System.out.println("indexOf(n, 1) -> " + s.indexOf('b'1));
  System.out.println("indexOf(n, 1) -> " + s.indexOf(981));
  }
  }

Output of the program:

C:\unique>java IndexOfChar
Honesty is the best Policy
indexOf(n, 1) -> 15
indexOf(n, 1) -> 15

C:\unique>

Download this example.