String Handling – indexOf()

indexOf()

syntax :-

int indexOf(char ch);

 

indexOf() is  standard inbuilt method of String class. It used to find the position of given character, if the given character is present in invoking string then it returns index number of the given character otherwise returns -1;

 

class Example

{

public static void main ( String arg[ ])

{

String s1 = “Code project“;

int n =s1.indexOf (‘P’);

System.out.println(n);

System.out.println(s1.indexOf(‘u’) );

System.out.println(s1.indexOf(’b’));

System.out.println(s1.indexOf(‘r’));

}

}

output:-

6

-1

-1

 

 

Related Links

 

Leave a Reply

Your email address will not be published. Required fields are marked *