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);
}
}
Try it online!
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.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With