Java String compareTo() method is used to compare two strings lexicographically. This method is declared in Comparable interface. Since String implements Comparable interface, it provides compareTo() method implementation.
Java String compareTo()方法用于按字典顺序比较两个字符串。 此方法在Comparable接口中声明。 由于String实现Comparable接口,所以它提供了compareTo()方法的实现。
Java字符串compareTo (Java String compareTo)
Java String class has two variants of compareTo()
method.
Java String类具有compareTo()
方法的两个变体。
compareTo(String anotherString)
: This compareTo method compares the String object with the String argument passed lexicographically.If String object precedes the argument passed, it returns negative integer and if String object follows the argument String passed, it returns a positive integer.
It returns 0 when both the String have same value, in this case
equals(String str)
method will return true.The comparison is based on the Unicode value of each character in the strings. You should check String class source code to check how this method works.
compareTo(String anotherString)
:此compareTo方法将String对象与按字典顺序传递的String参数进行比较。如果String对象在传递的参数之前,则返回负整数;如果String对象在传递的参数String之后,则返回正整数。
当两个String具有相同的值时,它返回0,在这种情况下
equals(String str)
方法将返回true。比较是基于字符串中每个字符的Unicode值。 您应该检查String类源代码以检查此方法的工作方式。
compareToIgnoreCase(String str)
: This compareTo method is similar to the first one, except that it ignores the case. It uses StringCASE_INSENSITIVE_ORDER
Comparator for case insensitive comparison.If the return value of this method is 0 then
equalsIgnoreCase(String str)
will return true. This method returns a negative integer, zero, or a positive integer as the specified String is greater than, equal to, or less than this String, ignoring case considerations.compareToIgnoreCase(String str)
:此compareTo方法类似于第一个方法,只是它忽略大小写。 它使用StringCASE_INSENSITIVE_ORDER
比较器进行不区分大小写的比较。如果此方法的返回值为0,则
equalsIgnoreCase(String str)
将返回true。 当指定的String大于,等于或小于此String时,此方法将返回负整数,零或正整数,而忽略大小写考虑。
Java字符串compareTo示例 (Java String compareTo Example)
Let’s see a small java class explaining the usage of java string compareTo methods.
让我们看一个小的Java类,解释Java字符串compareTo方法的用法。
package com.journaldev.util;
public class StringCompareToExample {
/**
* This class show String compareTo examples
* @param args
*/
public static void main(String[] args) {
String str = "ABC";
System.out.println(str.compareTo("DEF"));
System.out.println(str.compareToIgnoreCase("abc"));
}
}
The output of the above compareTo example program is shown below.
上面的compareTo示例程序的输出如下所示。
Above negative output is because “ABC” is lexicographically less than the “DEF”. The output is -3 because it compares the character values one by one. You can also confirm this with the below test program.
高于负输出的原因是,按字典顺序,“ ABC”比“ DEF”小。 输出为-3,因为它会一一比较字符值。 您也可以使用以下测试程序确认这一点。
public class Test {
public static void main(String[] args) {
char a = 'A';
char d = 'D';
System.out.println(a-d); //prints -3
}
}
So when “ABC” is compared to “DEF”, the character at first index is compared. Since they are different and ‘A’ comes before ‘D’ lexicographically, it returns a negative integer with the difference between them, hence the output is -3.
因此,当将“ ABC”与“ DEF”进行比较时,将比较第一个索引处的字符。 由于它们是不同的,并且按字典顺序,“ A”在“ D”之前,因此它返回一个负整数,它们之间的差是,因此输出为-3。
So if you compare “AABC” with “ADBC”, then also you will get the same output as -3. That’s all for Java String compareTo() method example. Note that this method is not the same as the String equals() method.
因此,如果将“ AABC”与“ ADBC”进行比较,那么您将获得与-3相同的输出。 Java字符串compareTo()方法示例就这些了。 请注意,此方法与String equals()方法不同。
Reference: Official Oracle Documentation
参考: Oracle官方文档