3:11 pm - Wednesday November 23, 6461

Compare two string in dictionary order int compareTo(String anotherStr)

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

                                 Compares two strings lexicographically(compiling a dictionary order)

Example Program


public class Model1 {

public static void main(String args[])

 {

     String s1 = "hello";

     String s2 = "hello";

     String s3 = "hi ";

     int k = s1.compareTo( s2 );

     System.out.println(k);

      

      k = s2.compareTo( s3 );

      System.out.println(k);

     

      k = s3.compareTo( s1 );

      System.out.println(k);

 }

Output

    0

   -4

     4

Description:

                                     In this program string value is assigned (s1,s2,s3). And comparing s1 to s2(s1=hello,s2=hello) both will be checked  by unicode(ASCII).For number of comparison same process will be continued.

Filed in: Java Examples

Comments are closed.