Dart 笔记 14 - 其它

StringBuffer

在 dart:core 库里。

在调用 toString() 之前,StringBuffer 不会生成新的字符串对象。

var sb = StringBuffer();
sb
  ..write('Use a StringBuffer for ')
  ..writeAll(['efficient', 'string', 'creation'], ' ')
  ..write('.');

var fullString = sb.toString();

assert(fullString ==
    'Use a StringBuffer for efficient string creation.');

writeAll 的第二个参数表示分隔符,将第一个参数列表里的数据用这个分隔符拼接起来。

正则表达式

在 dart:core 库里。

RegExp 类提供了与 JavaScript 正则表达式相同的功能。

var numbers = RegExp(r'\d+');

var allCharacters = 'llamas live fifteen to twenty years';
var someDigits = 'llamas live 15 to 20 years';

// contains() 可以使用正则
assert(!allCharacters.contains(numbers));
assert(someDigits.contains(numbers));

// 每匹配一次替换一次
var exedOut = someDigits.replaceAll(numbers, 'XX');
assert(exedOut == 'llamas live XX to XX years');
var numbers = RegExp(r'\d+');
var someDigits = 'llamas live 15 to 20 years';

// 判断是否有匹配
assert(numbers.hasMatch(someDigits));

// 匹配分组
for (var match in numbers.allMatches(someDigits)) {
  print(match.group(0)); // 15, then 20
}

Uri

在 dart:core 库里。

提供了对字符串进行编码和解码的函数,以便在 Uri 中使用。这些函数处理 uri 特有的字符,例如 &=。Uri 类还解析并公开 Uri 主机、端口、 协议等组件。

编解码完整的 URI

要对 URI 中具有特殊含义的字符(例如 /:&#)进行编解码,使用 encodeFull()decodeFull() 方法。这些方法适用于编解码完整的 URI,保留完整的特殊 URI 字符。

var uri = 'http://example.org/api?foo=some message';

var encoded = Uri.encodeFull(uri);
assert(encoded ==
    'http://example.org/api?foo=some%20message');

var decoded = Uri.decodeFull(encoded);
assert(uri == decoded);

只有 some 和 message 中间的空格被编码。

对 URI 组件进行编解码

要对 URI 中具有特殊含义的所有字符串字符进行编解码,包括但不限于 /&:,使用 encodeComponent()decodeComponent() 方法。

var uri = 'http://example.org/api?foo=some message';

var encoded = Uri.encodeComponent(uri);
assert(encoded ==
    'http%3A%2F%2Fexample.org%2Fapi%3Ffoo%3Dsome%20message');

var decoded = Uri.decodeComponent(encoded);
assert(uri == decoded);

每个特殊字符都被编码。例如,/ 被编码为 %2F

解析构建 URI

// 解析
var uri =
    Uri.parse('http://example.org:8080/foo/bar#frag');

assert(uri.scheme == 'http');
assert(uri.host == 'example.org');
assert(uri.path == '/foo/bar');
assert(uri.fragment == 'frag');
assert(uri.origin == 'http://example.org:8080');

// 构建
var uri = Uri(
    scheme: 'http',
    host: 'example.org',
    path: '/foo/bar',
    fragment: 'frag');
assert(
    uri.toString() == 'http://example.org/foo/bar#frag');

DateTime

在 dart:core 库里。

时区不是 UTC 就是当地时区。

// 获取当前日期和时间
var now = DateTime.now();

// 用指定年份创建对象 1/1/2000
var y2k = DateTime(2000); 

// 指定年月日
y2k = DateTime(2000, 1, 2); 

// 指定 UTC 时间 1/1/2000
y2k = DateTime.utc(2000); 

// 指定从 Unix 纪元(1970年1月1日0点)开始经过的毫秒值
y2k = DateTime.fromMillisecondsSinceEpoch(946684800000,
    isUtc: true);

// 解析 ISO 8601 时间值
y2k = DateTime.parse('2000-01-01T00:00:00Z');

// millisecondsSinceEpoch 返回毫秒值。1/1/2000, UTC
var y2k = DateTime.utc(2000);
assert(y2k.millisecondsSinceEpoch == 946684800000);

// 1/1/1970, UTC
var unixEpoch = DateTime.utc(1970);
assert(unixEpoch.millisecondsSinceEpoch == 0);

使用 Duration 类计算两个日期之间的差值,并将一个日期向前或向后移动:

var y2k = DateTime.utc(2000);

// 加 366 天
var y2001 = y2k.add(Duration(days: 366));
assert(y2001.year == 2001);

// 减 30 天
var december2000 = y2001.subtract(Duration(days: 30));
assert(december2000.year == 2000);
assert(december2000.month == 12);

// 返回两个日期的差值 Duration 对象
var duration = y2001.difference(y2k);
assert(duration.inDays == 366);

迭代

Iterable 和 Iterator 类支持 for-in 循环。

class Process {
  // Represents a process...
}

// 实现 Iterator
class ProcessIterator implements Iterator<Process> {
  @override
  Process get current => ...
  @override
  bool moveNext() => ...
}

// 继承 IterableBase
class Processes extends IterableBase<Process> {
  @override
  final Iterator<Process> iterator = ProcessIterator();
}

void main() {
  // Iterable objects can be used with for-in.
  for (var process in Processes()) {
    // Do something with the process.
  }
}

生成器

当需要延迟地生成一个值序列时,可以考虑使用生成器函数。Dart 内置支持两种生成器函数:

  • 同步生成器:返回 Iterable 对象
  • 异步生成器:返回 Stream 对象

要实现同步生成器函数,将函数体标记为 sync*,并使用 yield 语句传递值:

Iterable<int> naturalsTo(int n) sync* {
  int k = 0;
  while (k < n) yield k++;
}

要实现异步生成器函数,将函数体标记为 async*,并使用 yield 语句传递值:

Stream<int> asynchronousNaturalsTo(int n) async* {
  int k = 0;
  while (k < n) yield k++;
}

如果生成器是递归的,可以使用 yield* 来改进它的性能:

Iterable<int> naturalsDownFrom(int n) sync* {
  if (n > 0) {
    yield n;
    yield* naturalsDownFrom(n - 1);
  }
}

猜你喜欢

转载自blog.csdn.net/weixin_33805992/article/details/87217027
今日推荐