Reputation: 183
I'm currently creating something like emoji-keyboard in Android that will show list of emoticon
I don't want to use image, so I need Unicode character for emoji in Java source code to show emoticon in String output.
For example
Unicode Name: FACE WITH TEARS OF JOY š
C/C++/Java source code: "\uD83D\uDE02"
http://www.fileformat.info/info/unicode/char/1f602/index.htm
I need Java Unicode like this "\uD83D\uDE02"
because when I output Label with
Label.setText("\uD83D\uDE02");`
it works and shows FACE WITH TEARS OF JOY š
I've already googled this and found this list, I just don't understand how \uD83D\uDE02
was generated.
Upvotes: 3
Views: 20626
Reputation: 340138
Java source code supports all 149,813ā Unicode characters directly. No need to escape.
String x = "š" ;
System.out.println ( "x = " + x );
See this code run at Ideone.com.
x = š
Unfortunately, as of 2024, some IDEs such as IntelliJ are buggy in their lack of support for pasting some characters. This is purely a flaw in the IDE, not a problem with Java nor the javac compiler. My workaround: Paste the problematic character(s) as a Comment in the code-editor, then select and drag into the line of code.
Your 1F602
input is a hexadecimal number, equivalent to 128,514 in decimal.
You can use that hex number as a numeric literal in Java. Prefix with 0x
to distinguish from decimal literal.
int codePointHex = 0x1F602;
String x = Character.toString ( codePointHex );
System.out.println ( "x = " + x );
x = š
You can use the decimal number as a literal, rather than hex. Same result.
int codePointHex = 128_514;
String x = Character.toString ( codePointHex );
System.out.println ( "x = " + x );
x = š
char
Beware: The char
type has been essentially broken since Java 2, and legacy since Java 5. As a 16-bit value, char
is physically incapable of representing most characters. Methods such as String#length
can return unuseful results.
System.out.println ( "š".length ( ) ); // Not useful.
2
Instead, use Unicode code point integer numbers to work with individual characters.
System.out.println ( "š".codePoints ( ).count ( ) );
1
See the code point numbers.
"š".codePoints ().forEach ( System.out :: println );
128514
You can ask about a character, via its code point number.
Character.isEmoji ( 128_514 ) // Or pass 0x1F602.
true
Character.getName ( 0x1F602 ) // Or pass 128_514.
FACE WITH TEARS OF JOY
ā Java 22 & Java 23 support Unicode 15.1 which defines 149,813 characters. Unicode 16.0 defines 154,998 characters.
Upvotes: 0
Reputation: 18834
U+1F602
is an Unicode codepoint. Java can read these.
System.out.println(new StringBuilder().appendCodePoint(0x1F602).toString());
If you really need to convert it to the other kind of Unicode scapes, you can iterate through all the chars, and write those hex codes to the output:
for(char c : new StringBuilder().appendCodePoint(0x1F602).toString().toCharArray()) {
System.out.print("\\u" + String.valueOf(Integer.toHexString(c)));
}
System.out.println();
Upvotes: 3