日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > java >内容正文

java

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

發(fā)布時(shí)間:2025/3/11 java 56 豆豆
生活随笔 收集整理的這篇文章主要介紹了 在Java中使用Collat​​or和String类进行字符串比较 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

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

給定兩個(gè)字符串,我們必須使用Java中分類的Collat??or和String進(jìn)行比較。

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類 -比較兩個(gè)字符串,我們使用compare()方法-返回第一個(gè)不同字符的差,可能是正值,負(fù)值和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類 -比較兩個(gè)字符串,我們使用compareTo()方法 -返回第一個(gè)不同字符的差,可能是正值,負(fù)值和0。

使用Collat??or和String類進(jìn)行字符串比較的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 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類比較兩個(gè)String的用法。 Collat??or類類似于String類 ,但它更像是通用類,但其方法執(zhí)行與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"...

通過此代碼,我們使用了兩個(gè)字符串str1和str2 ,這兩個(gè)字符串被比較。 使用字符串類的compareTo()方法,我們得到輸出“比較字符串(使用String類):Apple位于Banana之前” ,這與應(yīng)用于col對象使用的collat??or類的方法相同。 當(dāng)我們使用整理器類的compare()方法時(shí),輸出為“比較字符串(使用整理器類):蘋果先于香蕉” ...

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

總結(jié)

以上是生活随笔為你收集整理的在Java中使用Collat​​or和String类进行字符串比较的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。