且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将Unicode编码的字符串转换为字母字符串

更新时间:2022-11-09 09:41:31

技术上做:

String myString = "u0048u0065u006Cu006Cu006F World";

自动将其转换为 "Hello World",因此我假设您正在从某个文件中读取字符串.为了将其转换为Hello",您必须将文本解析为单独的 unicode 数字,(获取 uXXXX 并获取 XXXX)然后执行 Integer.ParseInt(XXXX, 16) 获得一个十六进制值,然后将其转换为 char 以获得实际字符.

automatically converts it to "Hello World", so I assume you are reading in the string from some file. In order to convert it to "Hello" you'll have to parse the text into the separate unicode digits, (take the uXXXX and just get XXXX) then do Integer.ParseInt(XXXX, 16) to get a hex value and then case that to char to get the actual character.

完成此操作的一些代码:

String str = myString.split(" ")[0];
str = str.replace("\","");
String[] arr = str.split("u");
String text = "";
for(int i = 1; i < arr.length; i++){
    int hexVal = Integer.parseInt(arr[i], 16);
    text += (char)hexVal;
}
// Text will now have Hello