简体   繁体   中英

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

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?

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

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

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.

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.

名称是标准名称,可能会受到某些限制

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) ) );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM