我如何将unicode codepoint转换为他们的角色演示文稿?
如何将代表点的字符串转换为适当的字符?
例如,我想有一个函数获得U + 00E4并返回ä。
我知道在字符类中,我有一个函数toChars(int codePoint),它需要一个整数,但是没有函数需要这种types的字符串。
有一个内置函数,或者我必须做一些字符串转换,以获得我可以发送到该函数的整数?
谢谢,
大卫
代码点被写为以U+
为前缀的hex数字
所以,你可以做到这一点
int codepoint=Integer.parseInt(yourString.substring(2),16); char[] ch=Character.toChars(codepoint);
"\u00E4" new String(new int[] { 0x00E4 }, 0, 1);
这个例子不使用char []。
// this code is Kotlin, but you can write same thing in Java val sb = StringBuilder() val cp :Int // codepoint when { Character.isBmpCodePoint(cp) -> sb.append(cp.toChar()) Character.isValidCodePoint(cp) -> { sb.append(Character.highSurrogate(cp)) sb.append(Character.lowSurrogate(cp)) } else -> sb.append('?') }
到目前为止,我发现的最简单的方法是只投射代码点; 如果你只是期望每码点一个字符,那么这可能适合你。
int codepoint = ...; char c = (char)codepoint;