Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I write a 3 byte unicode character as string literal

Tags:

dart

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?

like image 230
Günter Zöchbauer Avatar asked Jun 15 '15 19:06

Günter Zöchbauer


People also ask

What is Unicode string literal?

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.

How do you write Unicode in text?

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.


1 Answers

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.
like image 161
Michael Liu Avatar answered Oct 27 '22 10:10

Michael Liu