Results 1 to 2 of 2
  1. #1
    sireesha is offline Member
    Join Date
    Nov 2007
    Posts
    50
    Rep Power
    0

    Question String comparison

    Hi friends,
    i have a doubt about string comparison.
    when we use the method like..
    Java Code:
    result=st1.compareTo(st2);
    then...
    result>0 if st1>st2
    result=0 if st1=st2
    result<0 if st1<st2

    how can we compare strings like numbers ?
    how this method works ?
    what is the use of string comparison ?
    please tell me.....
    thankq ...

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    You can use the compareTo method for class instances/objects that implement the Comparable interface, egs, String and Integer.
    Java Code:
    String s1 = "hello world";
    String s2 = "goodbye";
    int retVal = s1.compareTo(s2);
    boolean equal = s1.equals(s2);
    String s3 = "95";
    String s4 = "101";
    // Which number is larger?
    Integer i3 = Integer.valueOf(s3);
    int n4 = Integer.parseInt(s4);
    Integer i4 = Integer.valueOf(n4);
    int compare = i3.compareTo(i4);
    System.out.println("compare = " + compare);
    System.out.printf("i3 is %s i4%n",
                       (compare < 1) ? "less than" :
                       (compare > 1) ? "greater than" : "equal to");
    int n3 = i3.intValue();
    System.out.printf("n3 < n4 = %b%n", n3 < n4);

Similar Threads

  1. String comparison
    By abhiN in forum New To Java
    Replies: 2
    Last Post: 04-09-2008, 04:47 AM
  2. Comparison of Strings
    By Cero.Uno in forum New To Java
    Replies: 3
    Last Post: 02-11-2008, 02:46 AM
  3. Date comparison
    By Rageagainst20 in forum New To Java
    Replies: 0
    Last Post: 12-19-2007, 06:34 PM
  4. Parsing Dates for Comparison
    By Rageagainst20 in forum New To Java
    Replies: 1
    Last Post: 12-19-2007, 05:50 AM
  5. Replies: 0
    Last Post: 11-20-2007, 04:59 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •