equalsignorecase

equalsignorecase的使用方法有哪些

小亿
542
2023-08-01 17:38:06
栏目: 编程语言
开发者专用服务器限时活动,0元免费领! 查看>>

在Java中,equalsIgnoreCase()是用于比较两个字符串是否相等而不考虑大小写的方法。以下是equalsIgnoreCase()的使用方法:

  1. 直接使用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.");
}
  1. 将equalsIgnoreCase()与equals()方法结合使用:
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.");
}
  1. 与toLowerCase()方法结合使用:
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()方法比较字符串时不考虑大小写,即使字符串中包含其他字符,只要字母相同就会被认为是相等的。

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

相关推荐:Java中的equalsIgnoreCase()方法怎么使用

0
看了该问题的人还看了