如何将unicode代码点转换为其字符表示forms?

如何将代表代码点的字符串转换为适当的字符?

例如,我想要一个获得U+00E4并返回ä的函数。

我知道在字符类中我有一个函数toChars(int codePoint) ,它接受一个整数但是没有函数接受这种类型的字符串。

是否有内置函数或者我是否必须对字符串进行一些转换以获取可以发送给函数的整数?

代码点写为以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; 

你可以打印它们

 s='\u0645\u0635\u0631\u064a' print(s)