Java String : compareToIgnoreCase() Method
compareToIgnoreCase() method - Compare two strings lexicographically without considering case
The compareToIgnoreCase() method is used to compare two strings lexicographically, ignoring case differences.
Note: This method does not take locale into account, and will result in an unsatisfactory ordering for certain locales. The java.text package provides collators to allow locale-sensitive ordering.
Java Platform: Java SE 8 and above.
Syntax compareToIgnoreCase() method
compareToIgnoreCase(String str)
Parameters compareToIgnoreCase() method
Name | Description | Type |
---|---|---|
str | The String to be compared. | int |
Return Value compareToIgnoreCase() method
- A negative integer, zero, or a positive integer as the specified String is greater than, equal to, or less than this String, ignoring case considerations.
Return Value Type: int
Example: Java String compareToIgnoreCase() Method
The following example shows the usage of java String() method.
public class Exercise {
public static void main(String[] args)
{
System.out.println();
String str1 = "This is Python exercise 1";
String str2 = "This is Ruby Exercise 1";
System.out.println("String 1: " + str1);
System.out.println("String 2: " + str2);
// Compare the two strings.
int result = str1.compareToIgnoreCase(str2);
// Display the results of the comparison.
if (result < 0)
{
System.out.println("\"" + str1 + "\"" +
" is less than " +
"\"" + str2 + "\"");
}
else if (result == 0)
{
System.out.println("\"" + str1 + "\"" +
" is equal to " +
"\"" + str2 + "\"");
}
else // if (result > 0)
{
System.out.println("\"" + str1 + "\"" +
" is greater than " +
"\"" + str2 + "\"");
}
}
}
Output:
String 1: This is Python exercise 1 String 2: This is Ruby Exercise 1 "This is Python exercise 1" is less than "This is Ruby Exercise 1"
Java Code Editor:
Previous:compareTo Method
Next:concat Method
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/java-tutorial/string/string_comparetoignorecase.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics