STRING INSTANCE METHODS
Java String compareToIgnoreCase() method
compareToIgnoreCase() compares two strings lexicographically, ignoring letter case. It returns 0, a negative or positive int value.
Equal strings with different letter case
String pirmas = "JAVA";
String antras = "java";
int rezultatas = pirmas.compareToIgnoreCase(antras);
System.out.println(rezultatas);0
pirmasstores the text JAVA.antrasstores the text java.compareToIgnoreCase()compares strings without considering letter case.- Both strings are considered lexicographically equal, so it returns
0.
Why is compareToIgnoreCase() an instance method?
compareToIgnoreCase() is called on a specific string because it compares that string with another:
int rezultatas = pirmas.compareToIgnoreCase(antras);
We therefore do not write String.compareToIgnoreCase(...). The left-hand pirmas object is the base string, while antras is passed as the argument.
Method syntax
public int compareToIgnoreCase(String str)
| Part | Meaning |
|---|---|
| Calling object | pirmas in the line pirmas.compareToIgnoreCase(antras) |
| Parameter | str – the string to compare against, ignoring case |
| Return type | int |
Result 0 | The strings are equal ignoring case |
| Negative result | The calling string comes before the argument |
| Positive result | The calling string comes after the argument |
| Error | NullPointerExceptionwhen the argument is null |
Negative and positive results
String pirmas = "Java";
String antras = "Python";
System.out.println(pirmas.compareToIgnoreCase(antras));
System.out.println(antras.compareToIgnoreCase(pirmas));-6
6
Java comes before Python, so the first comparison returns a negative value. Reversing the strings makes it positive. In practice, only the sign matters: < 0, == 0 or > 0.
The difference from compareTo()
String pirmas = "java";
String antras = "Java";
System.out.println(pirmas.compareTo(antras));
System.out.println(pirmas.compareToIgnoreCase(antras));32
0
compareTo() is case-sensitive, so java and Java are considered different. compareToIgnoreCase() ignores this difference and returns 0.
Checking equality ignoring case
String vardas = "ADMIN";
String ivestis = "admin";
if (vardas.compareToIgnoreCase(ivestis) == 0) {
System.out.println("Tekstai sutampa");
} else {
System.out.println("Tekstai skiriasi");
}Tekstai sutampa
To check equality ignoring case, compareToIgnoreCase(...) == 0 works correctly. However, an even clearer method for this purpose is usually equalsIgnoreCase().
When one string is a prefix of the other
String pirmas = "Java";
String antras = "javascript";
System.out.println(pirmas.compareToIgnoreCase(antras));
System.out.println(antras.compareToIgnoreCase(pirmas));-6
6
If every character of the shorter string matches the longer string's prefix ignoring case, the result depends on the length difference. Java is six characters shorter than javascript, so the first comparison returns -6.
Sorting without considering case
import java.util.Arrays;
String[] zodziai = {"banana", "Apple", "cherry"};
Arrays.sort(zodziai, String.CASE_INSENSITIVE_ORDER);
System.out.println(Arrays.toString(zodziai));[Apple, banana, cherry]
Arrays.sort(zodziai, String.CASE_INSENSITIVE_ORDER) uses String case-insensitive comparison. This is useful when users should not need to care whether list entries begin with uppercase or lowercase letters.
Lithuanian letters
String pirmas = "Ąžuolas";
String antras = "ąžuolas";
System.out.println(pirmas.compareToIgnoreCase(antras));0
compareToIgnoreCase() can also compare non-English letters according to Java Unicode rules. In this example, Ąžuolas and ąžuolas are equal because only the first letter's case differs.
The argument cannot be null
String tekstas = "Java";
String kitas = null;
System.out.println(tekstas.compareToIgnoreCase(kitas));NullPointerException
If compareToIgnoreCase() receives null, Java cannot compare text with a nonexistent value. It throws NullPointerException.
Checking safely before comparison
String tekstas = "Java";
String kitas = null;
if (kitas != null) {
System.out.println(tekstas.compareToIgnoreCase(kitas));
} else {
System.out.println("Nėra su kuo palyginti");
}Nėra su kuo palyginti
If the string to compare may be null, check it first. This avoids a NullPointerException and lets the code display a clear result.
Common mistakes
- Expecting
compareToIgnoreCase()to returntrueorfalsewhen it returns anint. - Testing the exact returned number instead of checking
< 0,== 0or> 0. - Using the method for equality when
equalsIgnoreCase()is often clearer. - Assuming it converts the strings themselves to lowercase. It only compares them and does not change the objects.
- Passing a
nullargument to the method.
When should you use compareToIgnoreCase()?
compareToIgnoreCase() Use it to determine string order without considering case: sort user-entered words, compare commands, find which string comes first or create case-insensitive comparisons. For equality alone, it is usually clearer to use equalsIgnoreCase().