It can be done, without using StringBuilder
, with Unicode surrogate pair:
Surrogate characters are typically referred to as surrogate pairs.
They are the combination of two characters, containing a single code
point. To make the detection of surrogate pairs easy, the Unicode
standard has reserved the range from U+D800 to U+DFFF for the use of
UTF-16. No characters are assigned to code point values in this range.
When programs see a bit sequence that falls in this range, they
immediately—zip! zip!—know that they have encountered a surrogate
pair.
This reserved range is composed of two parts:
- High surrogates — U+D800 to U+DBFF (total of 1,024 code points)
- Low surrogates — U+DC00 to U+DFFF (total of 1,024 code points)
The following would print extraterrestrial alien emoji ():
int[] surrogates = {0xD83D, 0xDC7D};
String alienEmojiString = new String(surrogates, 0, surrogates.length);
System.out.println(alienEmojiString);
System.out.println("\uD83D\uDC7D"); // alternative way