formatWithMask method Null safety

String? formatWithMask(
  1. String mask
)

Inspired from Vincent van Proosdij Formats a String with a specific mask

Example

var string3 = 'esentisgreece';
var mask3 = 'Hello ####### you are from ######';
var masked3 = string3.formatWithMask(mask3); // returns 'Hello esentis you are from greece'

Implementation

String? formatWithMask(String mask) {
  if (this == null) {
    return null;
  }
  if (this!.isEmpty) {
    return null;
  }

  //var buffer = StringBuffer();
  var maskChars = mask.toArray();
  var index = 0;
  var out = '';
  for (var m in maskChars!) {
    if (m == '#') {
      if (index < this!.length) {
        out += this![index];
        index++;
      }
    } else {
      out += m;
    }
  }
  return out;
}