Java - String compareTo() 方法



描述

Java String compareTo() 方法用於按字典序比較兩個字串。它返回一個整數值,比較基於字串中每個字元的 Unicode 值。此 String 物件表示的字元序列與引數字串表示的字元序列按字典序進行比較。

  • 如果此 String 物件按字典序在引數字串之前,則結果為負整數。

  • 如果此 String 物件按字典序在引數字串之後,則結果為正整數。

  • 如果字串相等,則結果為零,當 equals(Object) 方法返回 true 時,compareTo 正好返回 0。

compareTo() 方法接受一個字串作為引數,並按字典序比較字串,字典序壓縮意味著按字母順序 (a-z)。在比較給定字串時,它不會丟擲任何異常。

語法

以下是Java String compareTo() 方法的語法 -

public int compareTo(String anotherString)

引數

  • anotherString - 這是要比較的字串。

返回值

  • 如果引數字串等於此字串,則此方法返回 0。

  • 如果此字串按字典序小於字串引數,則此方法返回負值。

  • 如果此字串按字典序大於字串引數,則此方法返回正值。

比較與其他字串相同的字串示例

如果給定的字串值相同,則 compareTo() 方法返回

在下面的程式中,我們同時使用"Hello""Hello" 兩個值例項化兩個字串類。然後,使用compareTo() 方法,我們嘗試檢查給定的字串是否按字典序相等。由於給定的字串按字典序等於字串引數,因此該方法返回 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 strings are: " + str1 + " and " + str2);
      
      //using the compare two method
      System.out.println("The string " + str1 + " is lexicographically equal to the string " + str2 + " or not? " + str1.compareTo(str2));
   }
}

輸出

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

The given strings are: Hello and Hello
The string Hello is lexicographically equal to the string Hello or not? 0

比較按字典序大於其他字串的字串示例

如果字串按字典序大於字串引數,則此方法返回正值。

在此示例中,我們同時建立了兩個字串字面量,其值為"Tutorials""Point"。使用toCompare() 方法,我們嘗試檢查給定的字串是否按字典序相等。由於給定的字串按字典序大於字串引數,因此該方法返回正值。

package com.tutorialspoint;

public class Compare {
   public static void main(String[] args) {
      
      //instantiate the string class
      String str1 = "Tutorials";
      String str2 = "Point";
      System.out.println("The given strings are: " + str1 + " and " + str2);
      
      //using the compare two method
      int compare = str1.compareTo(str2);
      System.out.println("The compareTo() method returns: " + compare);
      if (compare == 0) {
         System.out.println("The strings are lexicographically equal");
      } else if (compare > 0) {
         System.out.println("The first string is larger than the second string lexicographically.");
      } else {
         System.out.println("The first string is smaller than the second string lexicographically.");
      }
   }
}

輸出

以下是上述程式的輸出 -

The given strings are: Tutorials and Point
The compareTo() method returns: 4
The first string is larger than the second string lexicographically.

比較按字典序小於其他字串的字串示例

如果字串等於字串引數,但引數字串大小寫不同,則 compareTo() 方法返回正值。

在以下示例中,我們使用值 "hello" 和 "HELLO" 例項化字串類。使用 compareTo() 方法,我們嘗試按字典序比較字串。

package com.tutorialspoint;

public class Compare {
   public static void main(String[] args) {
      
      // instantiating the String class
      String str1 = new String("hello");// string
      String str2 = new String("HELLO");// string argument
      System.out.println("The given strings are: " + str1 + " and " + str2);
      
      //using the compareTo() method
      int val = str1.compareTo(str2);
      System.out.println("The str1.compareTo(str2) value is: " + val);
      if(val == 0) {
         System.out.println("The string " + str1 +  " and string argument " + str2 + " are lexicographically equal");
      } else if(val > 0) {
         System.out.println("The string " + str1 +  " is lexicographically greater than the string argument " + str2);
      } else {
         System.out.println("The string " + str1 +  " is lexicographically less than the string argument " + str2);
      }
   }
} 

輸出

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

The given strings are: hello and HELLO
The str1.compareTo(str2) value is: 32
The string hello is lexicographically greater than the string argument HELLO
java_lang_string.htm
廣告