Printing the same character several times without a loop
I use this way.
void main() {
print(new List.filled(40, "-").join());
}
So, your case.
main() {
const String FILLER = "-";
String headerTxt;
String headerBox;
headerTxt = 'OpenPGP signing notes from key `CD42FF00`';
headerBox = new List.filled(headerTxt.length, FILLER).join();
print(headerBox);
print(headerTxt);
print(headerBox);
// ...
}
Output:
-----------------------------------------
OpenPGP signing notes from key `CD42FF00`
-----------------------------------------
The original answer is from 2014, so there must have been some updates to the Dart language: a simple string multiplied by an int
works.
main() {
String title = 'Dart: Strings can be "multiplied"';
String line = '-' * title.length
print(line);
print(title);
print(line);
}
And this will be printed as:
---------------------------------
Dart: Strings can be "multiplied"
---------------------------------
See Dart String
's multiply *
operator docs:
Creates a new string by concatenating this string with itself a number of times.
The result of
str * n
is equivalent tostr + str + ...(n times)... + str
.Returns an empty string if
times
is zero or negative.