formatDigitPattern function

String formatDigitPattern(
  1. String text, {
  2. int digit = 4,
  3. String pattern = ' ',
})

每隔n位 加 pattern

Implementation

String formatDigitPattern(String text, {int digit = 4, String pattern = ' '}) {
  text = text.replaceAllMapped(RegExp("(.{$digit})"), (Match match) {
    return "${match.group(0)}$pattern";
  });
  if (text.endsWith(pattern)) {
    text = text.substring(0, text.length - 1);
  }
  return text;
}