String compareToIgnoreCase() method - Syntax and Examples
Learn how to use the Java String `compareToIgnoreCase()` method for case-insensitive string comparison.
This reference explains String compareToIgnoreCase() method - Syntax and Examples through its syntax, examples and boundaries. Use the linked course lessons when the surrounding Java concepts are unfamiliar.
Use this reference
Identify the documented behavior, read a complete example and check its inputs before applying it.
Introduction
The compareToIgnoreCase() method in Java is a handy tool for comparing two strings without worrying about the case (uppercase or lowercase) of the characters. Imagine you're sorting a list of names – do you want 'Alice' to come before 'bob' just because 'b' is alphabetically earlier than 'a'? Probably not! compareToIgnoreCase() lets you compare strings while ignoring those capitalization differences.
Syntax
public int compareToIgnoreCase(String str)
Parameters
| Parameter | Description |
|---|---|
str |
The string to be compared with the current string. |
Return Value
The compareToIgnoreCase() method returns an integer value based on the comparison:
- If the first string is lexicographically less than the second string, it returns a negative number.
- If the first string is lexicographically greater than the second string, it returns a positive number.
- If the strings are equal (ignoring case), it returns 0.
Examples
Example 1: Basic Comparison
This example demonstrates how to compare two simple strings, 'Java' and 'java', using compareToIgnoreCase().
public class Example1 {
public static void main(String[] args) {
String str1 = "Java";
String str2 = "java";
int result = str1.compareToIgnoreCase(str2);
System.out.println("Result: " + result); // Output the comparison result
}
}
Result: 0
Explanation: Because we're using compareToIgnoreCase(), Java ignores the case difference. Both strings are considered equal.
Example 2: Case-Sensitive vs. Case-Insensitive
This example shows the difference between compareTo() (case-sensitive) and compareToIgnoreCase() (case-insensitive).
public class Example2 {
public static void main(String[] args) {
String str1 = "Java";
String str2 = "java";
int caseSensitiveResult = str1.compareTo(str2);
int caseInsensitiveResult = str1.compareToIgnoreCase(str2);
System.out.println("Case-sensitive Result: " + caseSensitiveResult); // Output the case-sensitive result
System.out.println("Case-insensitive Result: " + caseInsensitiveResult); // Output the case-insensitive result
}
}
Case-sensitive Result: 32
Case-insensitive Result: 0
Explanation: compareTo() considers capitalization, so 'J' comes after 'j', resulting in a positive value. compareToIgnoreCase() ignores case, returning 0.
Example 3: Comparing with Different Strings
This example compares the string "Apple" with "banana", demonstrating how to handle strings that are not equal (ignoring case).
public class Example3 {
public static void main(String[] args) {
String str1 = "Apple";
String str2 = "banana";
int result = str1.compareToIgnoreCase(str2);
System.out.println("Result: " + result); // Output the comparison result
}
}
Result: -45
Explanation: 'A' comes before 'b', so 'Apple' is lexicographically less than 'banana'. compareToIgnoreCase() returns a negative number, indicating that the first string is smaller.
Example 4: Comparing Empty String
This example demonstrates comparison with an empty string. An empty string is considered to be smaller than any non-empty string.
public class Example4 {
public static void main(String[] args) {
String str1 = "";
String str2 = "Hello";
int result = str1.compareToIgnoreCase(str2);
System.out.println("Result: " + result); // Output the comparison result
}
}
Result: -28
Explanation: An empty string is considered smaller than any other non-empty string. The returned negative value signifies this.
Using this reference
Check the example’s types, inputs and documented behavior before copying it into an application. Some APIs require external libraries, files or platform services; consult their setup notes.
Related explanations: Java Data Types - Primitive and Non-Primitive, Java Class - Blueprint of Objects, Exceptions in Java Introduction.
Supporting documentation: Official Java learning guide.
Reference checks are optional and do not count toward course progress.