简体   繁体   English

Java将String转换为Unicode字符。 “ U + 1F600” =😀

[英]Java convert String to Unicode character. “U+1F600” = 😀

Please note that this question is not a duplicate. 请注意, 这个问题不是重复的。

I have a String like this: 我有一个像这样的字符串:

// My String
String myString = "U+1F600";
// A method to convert the String to a real character
String unicodeCharacter = convertStringToUnicode(myString);
// Then this should print: 😀
System.out.println(unicodeCharacter);

How can I convert this String to the unicode character 😀 ? 如何将此String转换为unicode字符😀 I then want to show this in a TextView . 然后,我想在TextView显示它。

What you are trying to do is to print the unicode when you know the code but as String... the normal way to do this is using the method 您想要做的是在知道代码但以String形式显示时打印unicode。通常的方法是使用方法

Character.toChars(int) Character.toChars(INT)

like: 喜欢:

System.out.print(Character.toChars(0x1f600));

now in you case, you have 现在,如果你有

String myString = "U+1F600";

so you can truncate the string removing the 1st 2 chars, and then parsing the rest as an integer with the method Integer.parseInt(valAsString, radix) 因此您可以截断字符串以删除第一个2个字符,然后使用Integer.parseInt(valAsString,radix)方法将其余字符解析为整数

Example: 例:

String myString = "U+1F600";
System.out.print(Character.toChars(Integer.parseInt(myString.substring(2), 16)));

尝试

yourTextVIew.setText(new String(Character.toChars(0x1F60B)));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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