Java: How to get Unicode name of a character (or its type category)?

拈花ヽ惹草 提交于 2019-12-13 11:36:43

问题


The Character class in Java defines methods which check a given char argument for equality with certain Unicode chars or for belonging to some type category. These chars and type categories are named.

As stated in given javadoc, examples for named chars are
HORIZONTAL TABULATION, FORM FEED, ...;
example for named type categories are
SPACE_SEPARATOR, PARAGRAPH_SEPARATOR, ...

However, being byte or int values instead of enums, the name of these types are "hidden" at runtime.

So, is there a possibility to get characters' and/or type categories' names at runtime?


回答1:


JDK7 will have a

String getName(int codepoint)

function (READ: a “static method” in class java.lang.Character) that will turn a codepoint into its official Unicode name.

Javadoc : http://docs.oracle.com/javase/7/docs/api/java/lang/Character.html#getName%28int%29




回答2:


Yes. Use the ICU4J library. It has a the entire UCD and an API to get things out of it.




回答3:


The Character class supports category info. Look at Character.getType(char) for the category. But i do not think, you can get the character names.




回答4:


I posted a .NET implementation here: Finding out Unicode character name in .Net

That should be very easy to port to Java. All you need is to download the Unicode Database: http://www.unicode.org/Public/UNIDATA/UnicodeData.txt, and the Java equivalent of a string splitting method and a Dictionary class, both of which I'm sure exist in Java.

This is a simple alternative to downloading some bloated library with tons of Unicode methods that Java and .NET probably both already support.




回答5:


The names are standard and may be used subject to certain limitations.




回答6:


For the character name, one can use Character.getName(int). However, for general category it is not so convenient:

// attach String names to Character constants
Map<Byte, String> unicodeCategories = new HashMap<>();
unicodeCategories.put(Character.COMBINING_SPACING_MARK, "Mc");
unicodeCategories.put(Character.CONNECTOR_PUNCTUATION, "Pc");
unicodeCategories.put(Character.CONTROL, "Cc");
unicodeCategories.put(Character.CURRENCY_SYMBOL, "Sc");
unicodeCategories.put(Character.DASH_PUNCTUATION, "Pd");
unicodeCategories.put(Character.DECIMAL_DIGIT_NUMBER, "Nd");
unicodeCategories.put(Character.ENCLOSING_MARK, "Me");
unicodeCategories.put(Character.END_PUNCTUATION, "Pe");
unicodeCategories.put(Character.FINAL_QUOTE_PUNCTUATION, "Pf");
unicodeCategories.put(Character.FORMAT, "Cf");
unicodeCategories.put(Character.INITIAL_QUOTE_PUNCTUATION, "Pi");
unicodeCategories.put(Character.LETTER_NUMBER, "Nl");
unicodeCategories.put(Character.LINE_SEPARATOR, "Zl");
unicodeCategories.put(Character.LOWERCASE_LETTER, "Ll");
unicodeCategories.put(Character.MATH_SYMBOL, "Sm");
unicodeCategories.put(Character.MODIFIER_LETTER, "Lm");
unicodeCategories.put(Character.MODIFIER_SYMBOL, "Sk");
unicodeCategories.put(Character.NON_SPACING_MARK, "Mn");
unicodeCategories.put(Character.OTHER_LETTER, "Lo");
unicodeCategories.put(Character.OTHER_NUMBER, "No");
unicodeCategories.put(Character.OTHER_PUNCTUATION, "Po");
unicodeCategories.put(Character.OTHER_SYMBOL, "So");
unicodeCategories.put(Character.PARAGRAPH_SEPARATOR, "Zp");
unicodeCategories.put(Character.PRIVATE_USE, "Co");
unicodeCategories.put(Character.SPACE_SEPARATOR, "Zs");
unicodeCategories.put(Character.START_PUNCTUATION, "Ps");
unicodeCategories.put(Character.SURROGATE, "Cs");
unicodeCategories.put(Character.TITLECASE_LETTER, "Lt");
unicodeCategories.put(Character.UNASSIGNED, "Cn");
unicodeCategories.put(Character.UPPERCASE_LETTER, "Lu");
// use the map to extract category name from the constant
char ch = 'a'; // OR int ch = Character.codePointAt("a", 0);
String category = unicodeCategories.get( (byte) (Character.getType(ch) ) );


来源:https://stackoverflow.com/questions/2443325/java-how-to-get-unicode-name-of-a-character-or-its-type-category

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!