June 22, 2010

Str.CompartTo(String str)

Often, it is not enough to simply know whether two strings are identical. For sorting applications, you need to know which is less than, equal to, or greater than the next. A string is less than another if it comes before the other in dictionary order. A string is greater than another if it comes after the other in dictionary order. The String method compareTo( ) serves this purpose. It has this general form:

 int compareTo(String str)
Here, str is the String being compared with the invoking String. The result of the comparison is returned and is interpreted as shown here:

Value                                    Meaning
Less than zero                     The invoking string is less than str.


Greater than zero                T he invoking string is greater than str.
Zero                                      The two strings are equal.




 If you want to ignore case differences when comparing two strings, use compareToIgnoreCase( ), shown here:
int compareToIgnoreCase(String str)
This method returns the same results as compareTo( ), except that case differences are ignored. This method was added by Java

2. You might want to try substituting it into the previous program. After doing so, "Now" will no longer be first.
More information: http://leepoint.net/notes-java/data/expressions/22compareobjects.html


Example :


  1. /*
  2.   Compare two Java Date objects using compareTo method example.
  3.   This example shows how to compare two java Date objects using compareTo method of
  4.   java Date Class.
  5. */
  6. import java.util.Date;
  7. public class CompareDateUsingCompareToExample{
  8. public static void main(String[] args) {
  9. //create first date object
  10. Date d1 = new Date();
  11. //make interval of 10 millisecond before creating second date object
  12. try{
  13. Thread.sleep(10);
  14. }catch(Exception e){
  15. }
  16. //create second date object
  17. Date d2 = new Date();
  18. System.out.println("First Date : " + d1);
  19. System.out.println("Second Date : " + d2);
  20. /*
  21.   Use compareTo method of java Date class to compare two date objects.
  22.   compareTo returns value grater than 0 if first date is after another date,
  23.   returns value less than 0 if first date is before another date and returns
  24.   0 if both dates are equal.
  25.   */
  26. int results = d1.compareTo(d2);
  27. if(results > 0)
  28. System.out.println("First Date is after second");
  29. else if (results < 0)
  30. System.out.println("First Date is before second");
  31. else
  32. System.out.println("Both dates are equal");
  33. }
  34. }
  35. /*
  36. TYPICAL Output Would be
  37. First Date : Sun Sep 09 19:50:32 EDT 2007
  38. Second Date : Sun Sep 09 19:50:32 EDT 2007
  39. First Date is before second
  40. */

No comments:

Post a Comment

I'm certainly not an expert, but I'll try my hardest to explain what I do know and research what I don't know.

My Favorite Site's List

#update below script more than 500 posts