Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use high-order unicode characters in java?

Tags:

java

unicode

How do I use unicode characters in Java, like the Negative Squared Latin Capital Letter E? Using "\u1F174" doesn't work as the \u escape only accepts 4 hex-digits.

like image 204
Niklas R Avatar asked Feb 05 '26 01:02

Niklas R


2 Answers

You need to specify it as a surrogate pair - two UTF-16 code units.

For example, if you copy and paste the character into my Unicode explorer you can see that U+1F174 is represented in UTF-16 code units as U+D83C U+DD74. (You can work this out manually, of course.) So you could write it in a Java string literal as:

String text = "\uD83C\uDD74";

Other options include:

String text = new StringBuilder().appendCodePoint(0x1f174).toString();
String text = new String(new int[] { 0x1f174 }, 0, 1);
char[] chars = Character.toChars(0x1f174);
like image 56
Jon Skeet Avatar answered Feb 07 '26 13:02

Jon Skeet


"\uD83C\uDD74"

Or indeed

"🅴"

Because Java characters represent UTF-16 units rather than actual Unicode characters, you need to represent it as a string, that will have the two UTF-16 surrogates.

like image 35
Jon Hanna Avatar answered Feb 07 '26 13:02

Jon Hanna



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!