本节目标

  • Runes
  • 基础知识 字符编码 ASCII、Unicode、UTF-8、UTF-16、UTF-32

环境

  • Dart 2.0.0

Runes

Runes 对象是一个 32位 字符对象,用来表示一个字。
这样设计也是考虑兼容 UTF-16 四个字节的情况。

lengthrunes.length 比较

1
2
3
4
5
6
7
String a = '👺';
print(a.length);
print(a.runes.length);

>> 输出
2 // 标识占 2 个 16 位字符
1 // 表示占 1 个 32 位字符

runes 是一个 32 位字符对象

操作 32-bit Unicode 字符

1
2
3
4
5
6
Runes b = new Runes('\u{1f596} \u6211');
var c = String.fromCharCodes(b);

或者

String c = '\u{1f596} \u6211'

如果非4个数值,需要用 {…}

返回 16-bit code units 的 codeUnitAt codeUnits

1
2
3
4
5
6
7
var a = '👺';
print(a.codeUnitAt(0));
print(a.codeUnits);

>> 输出
55357 // 第 1 位的 10 进制数值
[55357, 56442] // 显示 2 位的 10 进制数值

返回 32-bit Unicode 的 runes

1
2
3
4
5
6
var a = '👺';
print(a.runes);

>> 输出

(128122) // 显示 32 位的 10 进制数值

String 操作整理

名称 说明
codeUnitAt 某个字符的码 10进制
fromCharCodes Runes 转 String 工厂函数
runes 返回字对象

基础知识字符集

ASCII

非 ASCII 中的 GB2312、GBK

Unicode、UTF-8、UTF-16、UTF-32

代码

参考


© 猫哥

https://ducafecat.tech