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. intReturn Value compareToIgnoreCase() method
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
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4