freetimestudys@gmail.com          +91-7097633380
  Login Here                        

CompareTo(string anotherstring) and compareToIgnoreCase():-

1) By using compareTo() we are comparing two strings character by character such type of checking is called lexicographically checking or dictionary checking.
2) compareTo() is return three values as outcome
a. zero (if both are equal)
b. positive (first string first character is having big character compare to second string )
c. negative(first string first character small character compare to second String )
3) compareTo() method comparing two string with case sensitive.
4) By using above method we are comparing two strings character by character by ignoring case.

String CompareTo progam

class sup
{
public static void main(String... ratan)
{
String str1="venkat";
String str2="freetimestuy";
String str3="Venkatreddy";
String str4="VENKAT";
String str5="FREETIMESTUDY";
System.out.println(str1.compareTo(str2));
System.out.println(str1.compareTo(str4));
System.out.println(str2.compareTo(str4));
System.out.println(str4.compareTo(str2));
System.out.println(str1.compareTo(str4));
System.out.println(str1.compareTo(str5));
System.out.println(str1.compareToIgnoreCase(str4));
System.out.println(str2.compareToIgnoreCase(str4));
System.out.println(str1.compareToIgnoreCase(str3));
}
};

intput and output

output:16,, 32,, 16,, -16,, 32,, 48,, 0,, -16,, -5