Java - 从 unicode 转换为字符串?

标签 java string unicode character

我可以轻松创建一个 unicode 字符并使用以下代码行打印它

String uniChar = Character.toString((char)0000);
System.out.println(uniChar);

但是,现在我想检索上面的数字,添加3,并打印出数字0003对应的新unicode字符。有没有办法让我检索 unichar 的实际字符串?就像“\u0000”一样?这样我就可以只对“0000”进行子字符串化,将其转换为 int,添加 3,然后反转整个过程。

最佳答案

我认为您正在寻找 String#codePointAt :

Returns the character (Unicode code point) at the specified index. The index refers to char values (Unicode code units) and ranges from 0 to length()- 1.

If the char value specified at the given index is in the high-surrogate range, the following index is less than the length of this String, and the char value at the following index is in the low-surrogate range, then the supplementary code point corresponding to this surrogate pair is returned. Otherwise, the char value at the given index is returned.

例如( live copy ):

// String containing smiling face with smiling eyes emoji
String str = "😊";
// Get the code point
int cp = str.codePointAt(0);
// Show it
System.out.println(str + ", code point = U+" + toHex(cp));
// Increase it
++cp;
// Get the updated string (from an array of code points)
String updated = new String(new int[] { cp }, 0, 1);
// Show it
System.out.println(updated + ", code point = U+" + toHex(cp));

(toHex 只是 return Integer.toString(n, 16).toUpperCase();)

输出:

😊, code point = U+1F60A

😋, code point = U+1F60B

关于Java - 从 unicode 转换为字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34383211/

相关文章:

c# - 如何从字符串中删除表情符号字符?

java - 使用 apache POI 按值合并动态单元格

java - 如何从 Axis 1.4 stub 打印 XML 请求和响应

java - 如何创建批处理文件并使用参数执行 jar 文件

python - 如何询问用户特定的字符串并循环直到输入有效的输入?

python - Unicode 字符串适用于 python2,但不适用于 python3

java - Spring Data MongoDB 审计不适用于嵌入式文档

java - 如何用不同的字符串替换字符串中的特定字符?

java - 如何将名字更改为姓氏逗号名字java

ruby - 如何使用正则表达式匹配 unicode 字符代码点 12288