formatWithMask method

String formatWithMask(
  1. String mask, {
  2. String specialChar = '#',
})

Inspired from Vincent van Proosdij.

Formats the String with a specific mask.

You can assign your own specialChar, defaults to '#'.

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, {String specialChar = '#'}) {
  if (this.isBlank) {
    return this;
  }

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