Java 程序用于忽略大小写来检查两字符串之间的等式


在 Java 中使用 equalsIgnoreCase() 忽略大小写来检查两个字符串之间的等式。

假设以下内容是我们的两个字符串。

String one = "rocky";
String two = "Rocky";

这两个字符串相等,但大小写不同。由于此方法忽略大小写,因此这两个字符串都被视为相等。

在此处,我们检查的是同一问题。

if(one.equalsIgnoreCase(two)) {
    System.out.println("String one is equal to two (ignoring the case) i.e. one==two");
}else{
    System.out.println("String one is not equal to String two (ignoring the case) i.e. one!=two");
}

以下是一个完整示例。

示例

 在线演示

public class Demo {
    public static void main(String[] args) {
       String one = "rocky";
       String two = "Rocky";
       if(one.equalsIgnoreCase(two)) {
          System.out.println("String one is equal to two (ignoring the case) i.e. one==two");
       }else{
          System.out.println("String one is not equal to String two (ignoring the case) i.e. one!=two");
       }
    }
}

输出

String one is equal to two (ignoring the case) i.e. one==two

更新于: 26-6 月-2020

188 浏览量

开启你的职业生涯

通过完成课程来获得认证

开始
广告
© . All rights reserved.