实现说明
底层就是类 UnicodeNameLookup和函数 main。
使用要点
- 每一步打印都带着自己的标签:
U+、U+0378 ->、U+2603 ->。
常见问题
运行「任意码点的 Unicode 名称」会打印什么?
文件本身就是完整程序,直接运行——输出行带有 U+、U+0378 ->、U+2603 -> 这类标签。
核心语句
String name = Character.getName(cp);
int unassigned = 0x0378;
String input = "aΩ中ß";
代码示例
public class UnicodeNameLookup {
public static void main(String[] args) {
// Character.getName reports the official Unicode name of a code point
String input = "aΩ中ß";
input.codePoints().forEach(cp -> {
String name = Character.getName(cp);
System.out.println("U+" + Integer.toHexString(cp).toUpperCase()
+ " -> " + (name == null ? "(unnamed)" : name));
});
// unassigned code points yield null, so the page falls back to (unnamed)
int unassigned = 0x0378;
System.out.println("U+0378 -> " + Character.getName(unassigned));
// build the character back from its code point number
int snowman = 0x2603;
System.out.println("U+2603 -> " + Character.getName(snowman)
+ ": " + Character.toString(snowman));
}
}