formatDigitPattern static method

String formatDigitPattern(
  1. String str,
  2. int digit,
  3. String pattern,
  4. {bool end = false}
)

每隔digit位加pattern

end true表示从尾开始

Implementation

static String formatDigitPattern(
  String str,
  int digit,
  String pattern, {
  bool end = false,
}) {
  if (end) {
    String temp = reverse(str);
    temp = formatDigitPattern(temp, digit, pattern);
    temp = reverse(temp);
    return temp;
  } else {
    str = str.replaceAllMapped(RegExp('(.{$digit})'), (Match match) {
      return '${match.group(0)}$pattern';
    });
    if (pattern.isNotEmpty && str.endsWith(pattern)) {
      str = str.substring(0, str.length - 1);
    }
    return str;
  }
}