KnowledgeBoat Logo

Computer Applications

Differentiate between compareTo() and compareToIgnore().

Java String Handling

3 Likes

Answer

compareTo()compareToIgnore()
It compares two strings lexicographically.It compares two strings lexicographically, ignoring the case of the characters in a string.
Example:
String str1 = "computer";
String str2 = "COMPUTER";
int res = str1.compareTo(str2);
System.out.println(res);

The output is 32 as 'c' and 'C' are treated differently.
Example:
String str1 = "computer";
String str2 = "COMPUTER";
int res = str1.compareToIgnore(str2);
System.out.println(res);

The output is 0 as case difference is ignored.

Answered By

3 Likes


Related Questions