在Java中使用Collat​​or和String类进行字符串比较

Given two strings and we have to compare them using Collator and String classed in Java.

给定两个字符串,我们必须使用Java中分类的Collat​​or和String进行比较。

Using Collator class – to compare two strings, we use compare() method – it returns the difference of first dissimilar characters, it may positive value, negative value and 0.

使用Collat​​or类 -比较两个字符串,我们使用compare()方法-返回第一个不同字符的差,可能是正值,负值和0。

Using String class – to compare two strings, we use compareTo() method – it returns the difference of first dissimilar characters, it may positive value, negative value and 0.

使用String类 -比较两个字符串,我们使用compareTo()方法 -返回第一个不同字符的差,可能是正值,负值和0。

使用Collat​​or和String类进行字符串比较的Java代码 (Java code for string comparison using Collator and String classes)

// importing Collator and Locale classes
import java.text.Collator;
import java.util.Locale;

public class Main {
    //function to print strings (comparing & printing)
    public static void printString(int diff, String str1, String str2) {
        if (diff < 0) {
            System.out.println(str1 + " comes before " + str2);
        } else if (diff > 0) {
            System.out.println(str1 + " comes after " + str2);
        } else {
            System.out.println(str1 + " and " + str2 + " are the same strings.");
        }
    }

    public static void main(String[] args) {
        // Creating a Locale object for US english 
        Locale USL = new Locale("en", "US");

        // Getting collator instance for USL (Locale) 
        Collator col = Collator.getInstance(USL);
        String str1 = "Apple";
        String str2 = "Banana";

        // comparing strings and getting the difference
        int diff = col.compare(str1, str2);

        System.out.print("Comparing strings (using Collator class): ");
        printString(diff, str1, str2);

        System.out.print("Comparing strings (using String class): ");
        diff = str1.compareTo(str2);
        printString(diff, str1, str2);
    }
}

Output

输出量

Comparing strings (using Collator class): Apple comes before Banana
Comparing strings (using String class): Apple comes before Banana

Code explanation:

代码说明:

The above code shows the use of Collator class to compare two Strings. The Collator class is similar to String class, but it is more of a general class but its methods do the same logical evaluation as String class.

上面的代码显示了使用Collat​​or类比较两个String的用法。 Collat​​or类类似于String类 ,但它更像是通用类,但其方法执行与String类相同的逻辑求值。

It this code we have used two Strings str1 and str2 that are two be compared. Using the compareTo() method of string class we get the output "Comparing strings (using String class): Apple comes before Banana", which is same as when applied to the methods of collator class used using col object. The output when we use the compare() method of the collator class is "Comparing strings (using Collator class): Apple comes before Banana"...

通过此代码,我们使用了两个字符串str1和str2 ,这两个字符串被比较。 使用字符串类compareTo()方法,我们得到输出“比较字符串(使用String类):Apple位于Banana之前” ,这与应用于col对象使用的collat​​or类的方法相同。 当我们使用整理器类的compare()方法时,输出为“比较字符串(使用整理器类):苹果先于香蕉” ...

翻译自: https://www.includehelp.com/java-programs/string-comparison-using-collator-and-string-classes-in-java.aspx

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值