提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
语言敏感的字符串比较
import java.text.Collator; import java.util.Locale; public class Main { /* *来 自 时代Java - nowjava.com */ public static void main(String[] args) { // Create a Locale object for US Locale USLocale = new Locale("en", "US"); // Get collator instance for US Collator c = Collator.getInstance(USLocale); String str1 = "cat"; String str2 = "Dog"; int diff = c.compare(str1, str2); System.out.print("Comparing using Collator class: "); /** * n o w j a v a . c o m - 时代Java 提 供 **/ print(diff, str1, str2); System.out.print("Comparing using String class: "); diff = str1.compareTo(str2); print(diff, str1, str2); } public static void print(int diff, String str1, String str2) { if (diff > 0) { System.out.println(str1 + " comes after " + str2); } else if (diff < 0) {