克拉!
我想"美化"我的一个Dart脚本的输出,如下所示:
-----------------------------------------
OpenPGP signing notes from key `CD42FF00`
-----------------------------------------
<Paragraph>
Run Code Online (Sandbox Code Playgroud)
我想知道在Dart中是否有一种特别简单和/或优化的方式来打印相同的角色x时间.在Python中,print "-" * x会打印-字符x时间.
从这个答案中学习,为了这个问题的目的,我编写了以下最小代码,它使用了核心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));
// ...
}
Run Code Online (Sandbox Code Playgroud)
这给出了预期的输出,但在Dart中有更好的方法来打印字符(或字符串)x次数吗?在我的例子中,我想打印- …
dart中包io的stdout()函数的正确用法是什么?我写了这样一个剧本:
#import('dart:io');
void main() {
print("Hello World");
stdout();
}
Run Code Online (Sandbox Code Playgroud)
我得到以下输出:
firen @ firen-VirtualBox:〜/ Downloads $ ./dart/dart-sdk/bin/dart ./dart/workspace/test/text.dart Hello world未处理的异常:对象不是闭包0.函数:':: main' url:'/ home/firen/Downloads/dart /workspace/test/text.dart'行:6 col:7