在Java中使用Collator和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 differenceint 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 BananaCode 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中使用Collator和String类进行字符串比较的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: css 隐藏元素 显示元素_使用CSS打
- 下一篇: c语言i++和++i程序_使用C ++程