Java - String compareToIgnoreCase() 方法



描述

Java String compareToIgnoreCase() 方法用於按字典順序比較兩個字串,忽略大小寫差異。字典順序比較意味著比較可以按字母順序(a-z)進行。

compareToIgnoreCase() 方法接受一個字串引數,該引數儲存字串引數的值。如果字串在字典上相等,則返回一個整數值;如果當前字串在字典上大於字串引數,則返回正值;否則返回負值

注意 - compareToIgnoreCase() 方法不區分大小寫,因為它忽略字元的大小寫。

語法

以下是Java String compareToIgnoreCase() 方法的語法:

public int compareToIgnoreCase(String str)

引數

  • str − 這是要比較的字串。

返回值

此方法返回一個負整數、零或正整數,具體取決於指定的字串大於、等於或小於此字串(忽略大小寫)。

忽略大小寫比較兩個相等字串的示例

如果當前字串和給定字串的值相同,則 compareToIgnoreCase() 方法返回零 (0)

在下面的程式中,我們用值“Hello”“Hello”例項化字串類。使用compareToIgnoreCase() 方法,我們嘗試比較字串是否在字典上相等。由於字串在字典上相等,因此它返回0

package com.tutorialspoint;

public class Compare {
   public static void main(String[] args) {
      
      //Instantiate the string class
      String str1 = new String("Hello");
      String str2 = new String("Hello");
      System.out.println("The given string values are: " + str1 + " and " + str2);
      
      // using the compareToIgnoreCase() method
      int compare = str1.compareToIgnoreCase(str2);
      System.out.println("The given strings are lexicographically equal. " + compare);
   }
}

輸出

執行上述程式後,將產生以下結果:

The given string values are: Hello and Hello
The given strings are lexicographically equal. 0

忽略大小寫比較兩個不相等字串的示例

如果給定的字串值與字串引數不同,則此方法返回一個負值。

在下面的示例中,我們使用值“Java”“Programming”建立一個字串類的物件。使用compareToIgnoreCase() 方法,我們嘗試按字典順序比較字串。

package com.tutorialspoint;

public class Compare {
   public static void main(String[] args) {
   	
      //creating an object of the string class
      String str1 = new String("Java");
      String str2 = new String("Programming");
      System.out.println("The given string values are: " + str1 + " and " + str2);
      
      // using the compareToIgnoreCase() method
      int compare = str1.compareToIgnoreCase(str2);
      System.out.println("The compareToIgnoreCase() method returns: " + compare);
      System.out.println("The given strings are not lexicographically equal.");
   }
}

輸出

以下是上述程式的輸出:

The given string values are: Java and Programming
The compareToIgnoreCase() method returns: -6
The given strings are not lexicographically equal

忽略大小寫比較兩個相等字串的示例

如果給定的字串值相同但大小寫不同,則 compareToIgnoreCase() 返回

在下面的程式中,我們建立了值為“Java”和“JAVA”的字串文字。使用 compareToIgnoreCase() 方法,我們嘗試按字典順序比較字串。由於該方法忽略大小寫,因此它返回

package com.tutorialspoint;

public class Compare {
   public static void main(String[] args) {
      
      //creating an object of the string class
      String str1 = new String("Java");
      String str2 = new String("JAVA");
      System.out.println("The given string values are: " + str1 + " and " + str2);
      
      // using the compareToIgnoreCase() method
      int compare = str1.compareToIgnoreCase(str2);
      System.out.println("The compareToIgnoreCase() method returns: " + compare);
      if(compare == 0) {
         System.out.println("The given strings are lexicographically equal.");
      } else if(compare > 0) {
         System.out.println("The given string is lexicographically greater than the string argument.");
      } else {
         System.out.println("The given string is lexicographically less than the string argument.");
      }
   }
}

輸出

程式執行後,將返回以下輸出:

The given string values are: Java and JAVA
The compareToIgnoreCase() method returns: 0
The given strings are lexicographically equal.
java_lang_string.htm
廣告