3:13 pm - Friday December 16, 9278

Search index of a character from given position lastIndexOf(int ch,int fromIndex)

CANDID INDUSTRIAL TRAINING -- CHENNAI
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 occurrence of the specified character within a specified range. If the specified character placed as many times within the specified range means then it returns the last occurrence of the character. It allows case sensitive.

Example Program 1

public class Lstof

{
<p style="text-align: left;">             public static void main(String args[])</p>
{

String s="String Handling Functions";

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

String s1="g";

String s2="h";

System.out.println("Occurrence of the character"+" "+s1+” “+"in the given string:"+s.lastIndexOf(s1,7)+"\n");

System.out.println("Occurrence of the character"+" "+s2+” “+"in the given string is:"+s.lastIndexOf(s2,10));

}

}

Output

       Given String:String Handling Functions

       Occurrence of the character g in the given string is:5

       Occurrence of the character h in the given string is:-1

Example Program 2

public class Lstof

{

public static void main(String args[])

{

String s="String Handling Functions";

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

String s1="g";

String s2="H";

System.out.println("Occurrence of the character"+" "+s1+” “+"in the given string

is:"+s.lastIndexOf(s1,7)+"\n");

System.out.println("Occurrence of the character"+" "+s2+” “+"in the given string

is:"+s.lastIndexOf(s2,10));

}

}

Output

          Given String:String Handling Functions

          Occurrence of the character g in the given string is:5

          Occurrence of the character H in the given string is:7

Description

                            Here the character will be specified with range so it returns the last occurrence within the specified range. If the given string is in uppercase but the specified character in lowercase means it returns negative values.

Filed in: Java Examples

Comments are closed.