JAVA中字符串比较equals()和equalsIgnoreCase()的区别
1、使用equals( )方法比较两个字符串是否相等(区分大小写)
2、使用equalsIgnoreCase( )方法比较两个字符串是否相等(不区分大小写)
boolean equalsIgnoreCase(String str)
下面的例子说明了equals( )和equalsIgnoreCase( )方法:
class equalsTest {
public static void main(String args[]) {
String s1 = "Hello";
String s2 = "Hello";
String s3 = "HELLO";
System.out.println(s1 + " equals " + s2 + " -> " + s1.equals(s2));
System.out.println(s1 + " equals " + s3 + " -> " + s1.equals(s3));
System.out.println(s1 + " equalsIgnoreCase " + s3 + " -> " + s1.equalsIgnoreCase(s3));
}
}
该程序的输出如下所示:
Hello equals Hello -> true
Hello equals HELLO -> false
Hello equalsIgnoreCase HELLO -> true