Saturday 14 June 2014

compareToIgnoreCase (String str)

public int compareToIgnoreCase(String str)
Compares two strings lexicographically, ignoring case differences. Return a negative integer, zero, or a positive integer as the specified String is greater than, equal to, or less than this String, ignoring case considerations.

class StringCompareToIgnoreCase{
 public static void main(String args[]){
  String s1 = new String("abcd");
  String s2 = new String("ABCD");
  String s3 = new String("Efgh");
  
  System.out.println("s1 = " + s1);
  System.out.println("s2 = " + s2);
  System.out.println("s3 = " + s3);
  
  System.out.println("s1.compareTo(s2) = " + s1.compareToIgnoreCase(s2));
  System.out.println("s2.compareTo(s1) = " + s2.compareToIgnoreCase(s1));
  System.out.println("s1.compareTo(s3) = " + s1.compareToIgnoreCase(s3));
 }
}

Output
s1 = abcd
s2 = ABCD
s3 = Efgh
s1.compareTo(s2) = 0
s2.compareTo(s1) = 0
s1.compareTo(s3) = -4





Prevoius                                                 Next                                                 Home

No comments:

Post a Comment