Linux 拨号vps windows公众号手机端

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

lewis 9年前 (2016-11-11) 阅读数 8 #程序编程
文章标签 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.");
}
版权声明

本文仅代表作者观点,不代表米安网络立场。

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

热门