I would like to combine a "u"
with a String
that contains a Hex-Code so that I can print out a unicode character in the console.
I’ve tried something like this, but the console only prints regular text, eg uf600
:
ArrayList<String> arr = new ArrayList<String>(); emoji.codePoints() .mapToObj(Integer::toHexString) .forEach((n) -> arr.add(n)); // arr will contain hex strings for (int i = 1; i < arr.size(); i += 2) { System.out.println("\u" + arr.get(i)); }
Advertisement
Answer
In Java, u
exists only in the compiler, as a convenience to help you add unicode character literals in your source code. If at run time you create a string that contains u followed by hex digits, there is no mechanism in place to transform it into a single char
.
It sounds like you want to transform each code point separately to a string. Here is one way you can do that: use Character.toChars
to transform the code point to a char array, and then build a new string from the char array:
ArrayList<String> arr = new ArrayList<String>(); emoji.codePoints().mapToObj(Character::toChars).map(String::new) .forEach(arr::add)