Java String indexOf() method example.

Java 8Object Oriented ProgrammingProgramming

The IndexOf() method of the String class accepts a character as a parameter and it returns the index of the first occurrence of the specified character. In case if the String does not contain the specified character then it returns -1.

Example

Live Demo

import java.io.*;
public class Test {
   public static void main(String args[]) {
      String Str = new String("Welcome to Tutorialspoint.com");
      System.out.print("Found Index :" );
      System.out.println(Str.indexOf( 'o' ));
   }
}

Output

Found Index :4
raja
Published on 12-Feb-2018 06:19:54
Advertisements