Java - String indexOf() Method

Description:

This method has following different variants:






Syntax:

Here is the syntax of this method:
public int indexOf(int ch )

or

public int indexOf(int ch, int fromIndex)

or

int indexOf(String str)

or

int indexOf(String str, int fromIndex)

Parameters:

Here is the detail of parameters:



Return Value:

See the description.

Example:
import java.io.*;

public class Test {

   public static void main(String args[]) {
      String Str = new String("Welcome to www.dineshonjava.com");
      String SubStr1 = new String("Tutorials");
      String SubStr2 = new String("Sutorials");

      System.out.print("Found Index :" );
      System.out.println(Str.indexOf( 'o' ));
      System.out.print("Found Index :" );
      System.out.println(Str.indexOf( 'o', 5 ));
      System.out.print("Found Index :" );
      System.out.println( Str.indexOf( SubStr1 ));
      System.out.print("Found Index :" );
      System.out.println( Str.indexOf( SubStr1, 15 ));
      System.out.print("Found Index :" );
      System.out.println(Str.indexOf( SubStr2 ));
   }
}

This produces following result:

output:
Found Index :4
Found Index :9
Found Index :-1
Found Index :-1
Found Index :-1


<<Previous <<   || Index ||   >>Next >>



Labels: