如何将 3 字节的 unicode 字符写为字符串文字
How can I write a 3 byte unicode character as string literal
的代码为例
对于双字节代码,如 Dingbats (2702 - 27B0)
'abcd\u2702efg'
工作正常,但对于像 \u1F601
这样的较长代码,这不起作用。
String.fromCharCode(0x1f601)
虽然有效。
main() {
print('abcd\u2702efg');
print('abcd\u1F601efg');
print(new String.fromCharCode(0x1f601));
}
在 DartPad
试试
有没有办法在 Dart 中将 U+1F601
写成字符串文字?
将字符代码括在花括号中:
print('abcd\u{1F601}efg');
来自 Dart 编程的 §16.5,"Strings"
语言规范,第二版:
Strings support escape sequences for special characters. The escapes are:
- ...
- \x HEX DIGIT1 HEX DIGIT2, equivalent to
\u{HEX DIGIT1 HEX DIGIT2}.
- \u HEX DIGIT1 HEX DIGIT2 HEX DIGIT3 HEX DIGIT4, equivalent
to \u{HEX DIGIT1 HEX DIGIT2 HEX DIGIT3 HEX 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.
对于双字节代码,如 Dingbats (2702 - 27B0)
'abcd\u2702efg'
工作正常,但对于像 \u1F601
这样的较长代码,这不起作用。
String.fromCharCode(0x1f601)
虽然有效。
main() {
print('abcd\u2702efg');
print('abcd\u1F601efg');
print(new String.fromCharCode(0x1f601));
}
在 DartPad
试试有没有办法在 Dart 中将 U+1F601
写成字符串文字?
将字符代码括在花括号中:
print('abcd\u{1F601}efg');
来自 Dart 编程的 §16.5,"Strings" 语言规范,第二版:
Strings support escape sequences for special characters. The escapes are:
- ...
- \x HEX DIGIT1 HEX DIGIT2, equivalent to \u{HEX DIGIT1 HEX DIGIT2}.
- \u HEX DIGIT1 HEX DIGIT2 HEX DIGIT3 HEX DIGIT4, equivalent to \u{HEX DIGIT1 HEX DIGIT2 HEX DIGIT3 HEX 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.