Java字符串compareTo()

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()方法的两个变体。

  1. 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类源代码以检查此方法的工作方式。

  2. compareToIgnoreCase(String str): This compareTo method is similar to the first one, except that it ignores the case. It uses String CASE_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方法类似于第一个方法,只是它忽略大小写。 它使用String CASE_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()方法不同。

GitHub Repository. GitHub Repository中检出更多核心Java示例。

Reference: Official Oracle Documentation

参考: Oracle官方文档

翻译自: https://www.journaldev.com/810/java-string-compareto

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值