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 瀏覽量

開啟你的職業生涯

透過完成課程來獲得認證

開始
廣告