【发布时间】:2014-03-06 19:50:26
【问题描述】:
我想“美化”我的一个 Dart 脚本的输出,如下所示:
-----------------------------------------
OpenPGP signing notes from key `CD42FF00`
-----------------------------------------
<Paragraph>
我想知道是否有一种特别简单和/或优化的方式在 Dart 中打印相同的字符 x 次。在 Python 中,print "-" * x 将打印 "-" 字符 x 次。
向this answer学习,为了这个问题,我编写了以下最小代码,它利用了核心Iterable类:
main() {
// Obtained with '-'.codeUnitAt(0)
const int FILLER_CHAR = 45;
String headerTxt;
Iterable headerBox;
headerTxt = 'OpenPGP signing notes from key `CD42FF00`';
headerBox = new Iterable.generate(headerTxt.length, (e) => FILLER_CHAR);
print(new String.fromCharCodes(headerBox));
print(headerTxt);
print(new String.fromCharCodes(headerBox));
// ...
}
这给出了预期的输出,但是 有没有更好的方法在 Dart 中打印一个字符(或字符串)x 次?在我的示例中,我想打印 "-" 字符 headerTxt.length 次。
【问题讨论】:
标签: dart pretty-print iterable