在Java中,equalsIgnoreCase()是用于比较两个字符串是否相等而不考虑大小写的方法。以下是equalsIgnoreCase()的使用方法:
String str1 = "Hello";
String str2 = "hello";
if (str1.equalsIgnoreCase(str2)) {
System.out.println("The strings are equal.");
} else {
System.out.println("The strings are not equal.");
}
String str1 = "Hello";
String str2 = "hello";
if (str1.equals(str2) || str1.equalsIgnoreCase(str2)) {
System.out.println("The strings are equal.");
} else {
System.out.println("The strings are not equal.");
}
String str1 = "Hello";
String str2 = "hello";
if (str1.toLowerCase().equals(str2.toLowerCase())) {
System.out.println("The strings are equal.");
} else {
System.out.println("The strings are not equal.");
}
注意:equalsIgnoreCase()方法比较字符串时不考虑大小写,即使字符串中包含其他字符,只要字母相同就会被认为是相等的。