⬅ Previous Topic
Java StringcompareTo()
methodNext Topic ⮕
Java Stringconcat()
methodcompareToIgnoreCase()
method⬅ Previous Topic
Java StringcompareTo()
methodNext Topic ⮕
Java Stringconcat()
methodThe 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.
public int compareToIgnoreCase(String str)
Parameter | Description |
---|---|
str |
The string to be compared with the current string. |
The compareToIgnoreCase()
method returns an integer value based on the 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.
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.
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.
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.
⬅ Previous Topic
Java StringcompareTo()
methodNext Topic ⮕
Java Stringconcat()
methodYou can support this website with a contribution of your choice.
When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.