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

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

  1. java 调用弗雷_JAVA API(一)String类和StringBuffer类

    1.String类和StringBuffer类 在程序中经常会用到字符串,所谓的字符串就是指一连串的字符,它是由多个单个字符连接而成的.字符串中可以包含任意字符,这些字符必须包含在一对双引号" ...

  2. Java中的String数据类型,String类(字符串)详解

    目录 第一章.String概述 1)String是什么 2)String长什么样 3)String的构造方法(声明方式) 第二章.String类的详解 1)String底层是什么 2)字符串存储的内存 ...

  3. java double 转string_double转string java中double类型如何转换为String类型

    double转string java中double类型如何转换为String类型 在我们做的项目中,有一个字符串的生成,我们需要double去拼接,结果发现了,拼接后的字符串,那个double值用科学 ...

  4. Java的API及Object类、String类、字符串缓存区

    Java 的API(API: Application(应用) Programming(程序) Interface(接口)) Object:Object类是Java语言中的根类,即所有类的父类. equ ...

  5. java实现linkstring,【JAVA SE基础篇】32.String类入门

    [JAVA SE基础篇]32.String类入门 1.字符串 1.String类又称作不可变字符序列 2.String位于java.lang包中,java程序默认导入java.lang包下所有的类 3 ...

  6. java在dog中定义name变量,组合构造 冯跃峰 java中组合的应用(不相干的类共同完成一个功能)+构造器回顾...

    java中组合的应用(不相干的类共同完成一个功能)+构造器回顾:今天我们了解到java的组合.可以说java组合是非常的重要的.可以对类进行封装,将底层的内容封装起来.甚至于java的继承都没有其重要 ...

  7. JAVA day13,14 API、Object类、日期时间类(long,Date,Calendar,DateFormat)、String类(字符串,可变长字符串)、正则表达式、包装类

    1.API API(Application Programming Interface),应⽤程序编程接⼝.Java API是⼀本程序员的"字 典",是JDK提供给我们使⽤的类的说 ...

  8. Java中的Type接口和Class类区别和联系

    Java中的Type接口和Class类有什么区别 Type是Class的父接口. Type 是 Java 编程语言中所有类型的公共高级接口.它们包括原始类型.参数化类型.数组类型.类型变量和基本类型. ...

  9. java进阶第二讲-数组、String类

    java进阶第二讲-数组.String类 1 回顾一下Object Object中的方法:public native int hashCode();带有native关键字的方法调用的是底层C++的dl ...

最新文章

  1. 爱可可推荐!关于竞赛思路,方法和代码实践,Datawhale数据竞赛Baseline开源分享!...
  2. 【C 语言】内存四区原理 ( 栈内存属性增长方向 | 栈内存开口方向 | 代码示例 )
  3. Errors running builder 'JavaScript Validator' on project
  4. 多线程的基本概念 (什么是cpu)
  5. iframe和HTML5 blob实现JS,CSS,HTML直接当前页预览
  6. 漫画 | 如何凭实力炒老板鱿鱼,并喜提N+1~
  7. Wordpress安装简要说明
  8. 学计算机的男孩子怎么追女孩子,男孩子追女孩子的套路,原来有这么多,快来学一学...
  9. 关于举办XX学院“XX杯”网页设计大赛的通知
  10. 机器学习第九篇:详解Adaboost算法
  11. 基于小波包的图像压缩及matlab实现,基于小波包的图像压缩及matlab实现精选.doc...
  12. 搞清axis的含义,这一篇就够了!
  13. c语言共享内存,在爷儿俩进程间使用共享内存(共享内容含指针)
  14. Python脚本可在Microsoft Excel中格式化数据
  15. java二进制 中文_Java 实现中文与二进制代码互转
  16. ThreadPoolExecutor 的三种提交任务方式
  17. ENVI5.1LC08大气校正前期步骤
  18. NOI题库练习1.5(07)
  19. 樊登读书会用事实说话读后感_用事实说话樊登读书笔记
  20. 海行Newlifest M1骨传导耳机开箱,这音质真的碉堡了

热门文章

  1. webpack 读取文件夹下的文件_webpack基本介绍及使用
  2. ModuleNotFoundError: No module named ‘torch.utils.serialization‘解决
  3. vscode连接远程服务器 SSH
  4. drbd(三):drbd的状态说明
  5. 迟到的年度总结,我们应该收放自如
  6. 11.python并发入门(part5 event对象)
  7. 20个编写现代CSS代码的建议
  8. php相应的扩展的对应链接地址
  9. Hapoxy+keepalived实现双主高可用负载均衡
  10. jQuery.grep()