扫描二维码下载沐宇APP

沐宇

微信扫码使用沐宇小程序

沐宇

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

扬州沐宇科技
2024-03-13 17:23:42
Java

在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.");
}

扫码添加客服微信