Initializing thieves characters in Java

I am trying to use some fancy characters in my Java code.

Character c = new Character('πŸ€€'); 

(If your web browser does not display a symbol, it is "1F000 πŸ€€ MAHJONG TILE EAST WIND", taken from here .

Java complains about "invalid character constant". What gives? I thought the Java character supports Unicode.

Also, is there a way to initialize a character by its Unicode value? Something like new Character('0x01F000') ?

+4
source share
2 answers

Characters without BMP (basic multilingual planes) cannot be represented as Java char (or, therefore, a Character ), because char is only a 16-bit unsigned integer. Non-BMP characters are represented using surrogate pairs in Java.

You will need to use a string ... but even then I suspect that you will need to provide a surrogate character pair explicitly. C # has an escape sequence \U , which is equivalent to \U , but for 32-bit values, but Java has nothing like this :(

Here is an alternative approach that allows you to use the Unicode value directly in your code:

 String x = new String(new int[] { 0x1f000 }, 0, 1); 

It's ugly, but it works ...

+6
source

Just an alternative, but you can also use:

 String str = new String(Character.toChars(0x1F000) ); 
+3
source

Source: https://habr.com/ru/post/1334760/


All Articles