pad method
Pads the string on the left and right sides if it's shorter than length
.
Padding characters will be truncated if they can't be evenly divided by length
.
var pad1 = 'abc'.pad(8); // ' abc '
var pad2 = 'abc'.pad(8, '=_'); // '=_abc=_='
var pad3 = 'abc'.pad(3); // 'abc'
Implementation
String pad(int length, [String chars = ' ']) {
var strLength = length != 0 ? this.length : 0;
if (length == 0 || strLength >= length) {
return this;
}
var mid = (length - strLength) / 2;
return (_createPadding(mid.floor(), chars) +
this +
_createPadding(mid.ceil(), chars));
}