String Handling – lastIndexOf ( )

  lastIndexOf ( )

Syntax
int lastIndexOf(char ch); 

 
lastIndexOf( ) is standard inbuilt method of String, it is used to search a given character (ch) from the last index number of invoking object if the given character is present then it returns its respective index number otherwise returns – 1;

class Example 1

{

public static void main (String arg[ ] )

{

String s1=”Code project”;

int n=s1.lastIndexOf(‘o’);

System.out.println (n);

System.out.println (s1.lastIndexOf(‘t’));

System.out.println (s1.lastIndexOf(‘b’));

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

}

}

output :-

7

12

-1

-1

 

 

Related Links

 

Leave a Reply

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