fillSpaceStr static method
字符串数组,填充空格保持字符串长度一致
@param strs 字符串列表
Implementation
static List<String> fillSpaceStr(List<String> strs) {
if (strs.isEmpty) return strs;
// 获取最长字符串的长度
int maxLength = strs.map((str) => str.length).reduce(max);
// 遍历处理每个字符串
List<String> result = strs.map((str) {
if (str.length == maxLength) return str;
// 计算需要补充的空格数量
int spacesToAdd = maxLength - str.length;
// 如果是奇数,后面多加一个空格
int frontSpaces = spacesToAdd ~/ 2;
int backSpaces = spacesToAdd - frontSpaces;
// 在字符串前后添加空格
return '${' ' * frontSpaces}$str${' ' * backSpaces}';
}).toList();
return result;
}