12:51 am - Thursday July 27, 2017

Search a position of a character from the last int lastIndexOf(int ch)

Corporate Training for Experienced Candidate
Struts | Hibernate | Spring | Java / J2EE
SOAP | RestFull | Design Pattern | more...
Ph: +91 72000 69003
137b, 2nd st, shanthi nagar, Chrompet, Chennai -600044

                      This method returns the last occurrence of the specified character in the given string  (i.e., it returns the occurrence position of the specified character). If the specified character placed number of times in a given string means it returns the last occurrence of the character.

Example Program

   public class lastindexof {

   public static void main(String args[])


     String s1 = "String Handling Functions";

     System.out.println("Given String:" + s1 + "\n");

     String s2 = "i";

     String s3 = "s";

     System.out.println("Last Occurrence of" + " " + s2 + " "

      + "in the given string      is:" + s1.lastIndexOf(s2) + "\n");

     System.out.println("Last Occurrence of" + " " + s3 + " "

      + "in the given string is:" + s1.lastIndexOf(s3));




         Given String:String Handling Functions

         Last Occurrence of i in the given string is:21

         Last Occurrence of s in the given string is:24


                      Specified characters in the above program is i and s. lastIndexOf(int ch) function returns the last occurrence position of the i and s in the given string “String Handling Functions”. Here i and s placed number of times in the string so it returns the last occurrence. Last index of i an s is 21 and 24.

Filed in: Z-A All Java Codes

Comments are closed.