Take for example the codes for these emoticons
For two-byte codes like Dingbats (2702 - 27B0)
'abcd\u2702efg'
works fine but for longer codes like \u1F601
this doesn't work.
String.fromCharCode(0x1f601)
works though.
main() {
print('abcd\u2702efg');
print('abcd\u1F601efg');
print(new String.fromCharCode(0x1f601));
}
Try at DartPad
Is there a way to write U+1F601
as a string literal in Dart?
If the character string literal has a prefix of N, the literal is treated as a Unicode string. When the N prefix is used, the characters in the literal are read as WCHAR characters. Any string literal with non-ASCII characters is treated as a Unicode literal by default.
Inserting Unicode characters To insert a Unicode character, type the character code, press ALT, and then press X. For example, to type a dollar symbol ($), type 0024, press ALT, and then press X.
Enclose the character code in curly braces:
print('abcd\u{1F601}efg');
From §16.5, "Strings", of the Dart Programming Language Specification, Second Edition:
Strings support escape sequences for special characters. The escapes are:
- ...
- \x HEX DIGIT1HEX DIGIT2, equivalent to \u{HEX DIGIT1HEX DIGIT2}.
- \u HEX DIGIT1HEX DIGIT2HEX DIGIT3HEX DIGIT4, equivalent to \u{HEX DIGIT1HEX DIGIT2HEX DIGIT3HEX DIGIT4}.
- \u{HEX DIGIT SEQUENCE} is the unicode scalar value represented by the HEX DIGIT SEQUENCE. It is a compile-time error if the value of the HEX DIGIT SEQUENCE is not a valid unicode scalar value.
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