hideNumber static method

String hideNumber(
  1. String str, {
  2. int start = 0,
  3. int? end,
  4. String replacement = '*',
})

使用replacement隐藏第startend的位数

Implementation

static String hideNumber(
  String str, {
  int start = 0,
  int? end,
  String replacement = '*',
}) {
  var buffer = StringBuffer();
  if (str.length <= 1) {
    return str.replaceRange(0, str.length, replacement);
  }
  if (end == null) {
    end = (str.length / 2).round();
  } else {
    if (end > str.length) {
      end = str.length;
    }
  }
  for (var i = 0; i < str.length; i++) {
    if (i >= end) {
      buffer.write(String.fromCharCode(str.runes.elementAt(i)));
      continue;
    }
    if (i >= start) {
      buffer.write(replacement);
      continue;
    }
    buffer.write(String.fromCharCode(str.runes.elementAt(i)));
  }
  return buffer.toString();
}