在Java语言中,忽略大小写有以下几种方法:
String str1 = "Hello"; String str2 = "hello"; if (str1.equalsIgnoreCase(str2)) { System.out.println("字符串相等"); } else { System.out.println("字符串不相等"); }
输出结果为:“字符串相等”
String str1 = "hello"; String str2 = "Hello"; int result = str1.compareToIgnoreCase(str2); if (result == 0) { System.out.println("字符串相等"); } else if (result < 0) { System.out.println("字符串1小于字符串2"); } else { System.out.println("字符串1大于字符串2"); }
输出结果为:“字符串相等”
String str1 = "hello"; String str2 = "Hello"; if (str1.matches("(?i)" + str2)) { System.out.println("字符串相等"); } else { System.out.println("字符串不相等"); }
输出结果为:“字符串相等”
这些方法都可以在比较字符串时忽略字符串的大小写差异,根据实际需求选择合适的方法使用。