Created
June 15, 2012 16:15
-
-
Save KL-7/2937338 to your computer and use it in GitHub Desktop.
ICU collation test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.ibm.icu.lang.UCharacter; | |
import com.ibm.icu.text.Collator; | |
public class CollationDemo { | |
private Collator collator; | |
public static void main(String arg[]) { | |
new CollationDemo().runDemo(); | |
} | |
public void runDemo() { | |
collator = Collator.getInstance(); | |
System.out.println(collationElement(UCharacter.toString(0x410) + UCharacter.toString(0x62))); | |
System.out.println(collationElement(UCharacter.toString(0x430) + UCharacter.toString(0x306) + UCharacter.toString(0x334))); | |
} | |
public String collationElement(String str) { | |
byte[] sortKey = collator.getCollationKey(str).toByteArray(); | |
return byteArrayToString(sortKey); | |
} | |
public String collationElement(int cp) { | |
return collationElement(UCharacter.toString(cp)); | |
} | |
public String byteArrayToString(byte[] bytes) { | |
StringBuilder sb = new StringBuilder(); | |
String delim = ""; | |
for (int b : bytes) { | |
sb.append(delim).append(Integer.toHexString(b & 0xFF)); | |
delim = ","; | |
} | |
return sb.toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment