我可以轻松创建一个 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/