How to put Unicode char U+1F604 in Java String? I attempted using
String s = "\u1F604";
but it equivalent to
String s = "\u1F60"+"4";
it was split into 2 chars.
How to put Unicode char U+1F604 in Java String? I attempted using
String s = "\u1F604";
but it equivalent to
String s = "\u1F60"+"4";
it was split into 2 chars.
DuncG's answer is a good way of doing it. The short explanation for this is that Unicode characters, by default, only take up 4 bytes, so the string literal escape only allows \u####
. However, emojis are surrogate pairs and Unicode has reserved U+D800
to U+DFFF
for these pairs, allowing 1024 x 1024 pair characters.
A different way of doing it that doesn't require converting into UTF-16 and encoding as a surrogate pair is to use Character.toChars(...)
:
public class Main {
public static void main(String[] args) {
String s = "Hello " + new String(Character.toChars(0x1f604)) + "!";
System.out.println(s);
}
}
The third variant, especially Character.toString(0x1f604)
:
public class Main {
public static void main(String[] args) {
String s1 = "Hello " + Character.toString(0x1f604) + "!"; // Since Java 11
String s2 = "Hello " + new String(new int[]{0x1f604}, 0, 1) + "!"; // < 11
System.out.println(s1 + " " + s2);
}
}
(Notice that in some other languages \U0001f604
might be used. In java \u
and \U
are the same.)
The UTF-16 encoding of your character U+1F604
is 0xD83D 0xDE04
, so it should be:
String s = "\uD83D\uDE04";
You can add this UTF-16 smiley face symbol to the string as a symbol itself, as a hexadecimal or decimal surrogate pair, or its supplementary code point.
// symbol itself
String str1 = "";
// surrogate pair
String str2 = "\uD83D\uDE04";
// surrogate pair to its supplementary code point value
int cp = Character.toCodePoint('\uD83D', (char) 0xDE04);
// since 11 - decimal codepoint to string
String str3 = Character.toString(cp);
// since 11 - hexadecimal codepoint to string
String str4 = Character.toString(0x1f604);
// output
System.out.println(str1 + " " + str2 + " " + str3 + " " + str4);
Output:
If you have a string representation of a hexadecimal value of a character, you can read a numeric value using Integer.parseInt
method.
// surrogate pair
char high = (char) Integer.parseInt("D83D", 16);
char low = (char) Integer.parseInt("DE04", 16);
String str1 = new String(new char[]{high, low});
// supplementary code point
int cp = Integer.parseInt("1F604", 16);
char[] chars = Character.toChars(cp);
String str2 = new String(chars);
// since 11
String str3 = Character.toString(cp);
// output
System.out.println(str1 + " " + str2 + " " + str3);
Output: