ICSE Class 10 Computer Applications Question 16 of 46

String Handling — Question 16

Back to all questions
16
Question

Question 3(v)

Differentiate between compareTo() and compareToIgnore().

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.