formatDigitPattern static method

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

每隔digit位加pattern

end true表示从尾开始 repeat 是否重复

Implementation

static String formatDigitPattern(
  String str,
  int digit,
  String pattern, {
  bool end = false,
  bool repeat = true,
}) {
  if (repeat) {
    if (end) {
      String temp = reverse(str);
      temp = formatDigitPattern(temp, digit, pattern, repeat: repeat);
      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;
    }
  } else {
    if (str.length < digit) {
      return str;
    }
    if (end) {
      var before = str.substring(0, str.length - digit);
      var after = str.substring(str.length - digit, str.length);
      return '$before$pattern$after';
    } else {
      var before = str.substring(0, digit);
      var after = str.substring(digit, str.length);
      return '$before$pattern$after';
    }
  }
}