kformatDigitPattern method

String? kformatDigitPattern({
  1. int digit = 4,
  2. String pattern = ' ',
})

每隔 x位 加 pattern。比如用来格式化银行卡

    "0123456789".kformatDigitPattern() => 0123 4567 89
    "0123456789".kformatDigitPattern( digit: 3, pattern: "_" => 012_345_678_9

Implementation

String? kformatDigitPattern({int digit = 4, String pattern = ' '}) {
  var file = this;
  if (file == null) return null;
  file = file.replaceAllMapped(RegExp('(.{$digit})'), (Match match) {
    return '${match.group(0)}$pattern';
  });
  if (file.endsWith(pattern)) {
    file = file.substring(0, file.length - 1);
  }
  return file;
}