java怎么比较两个字符串是否相等

1492
2024/3/5 17:23:42
栏目: 编程语言
开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

在Java中,可以使用equals()方法来比较两个字符串是否相等。示例如下:

String str1 = "Hello";
String str2 = "World";

if(str1.equals(str2)) {
    System.out.println("The two strings are equal.");
} else {
    System.out.println("The two strings are not equal.");
}

另外,还可以使用equalsIgnoreCase()方法来忽略大小写比较两个字符串是否相等。示例如下:

String str1 = "hello";
String str2 = "HELLO";

if(str1.equalsIgnoreCase(str2)) {
    System.out.println("The two strings are equal ignoring case.");
} else {
    System.out.println("The two strings are not equal ignoring case.");
}

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

推荐阅读: java深拷贝的三种实现方式