Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to put Unicode char in Java String? [duplicate]

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.

like image 305
Yeezh Avatar asked Mar 04 '26 19:03

Yeezh


2 Answers

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!

like image 53
hyper-neutrino Avatar answered Mar 06 '26 09:03

hyper-neutrino


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.)

like image 35
Joop Eggen Avatar answered Mar 06 '26 08:03

Joop Eggen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!