如何通过新的行来迭代字符串内容。

问题描述 投票:0回答:1

该方法 readAsString() 用一个字符串返回整个内容。

例如,这里是一个源文件。

#line1 // note, the carriage return here (new line symbol)
#line2

如何像源文件那样用行来区分内容,即用行来区分内容?

await file.readAsString().then(
    (value) {
      print(value); // the whole content will be printed

      // how to iterate the content by lines
      // i.e. process at first `#line1` and so on
    },
);
dart
1个回答
1
投票

使用 File.readAsLines:

await file.readAsLines().then(
    (List<String> lines) {
      for (var line in lines) {
        ...
      }
    },
);
© www.soinside.com 2019 - 2024. All rights reserved.