replace function

String replace(
  1. String string,
  2. dynamic pattern,
  3. dynamic replacement
)

Replaces occurrences of pattern in string with replacement. Returns the modified string.

Implementation

String replace(String string, dynamic pattern, dynamic replacement) {
  Pattern patternObj = pattern is RegExp ? pattern : RegExp(pattern.toString());
  if (replacement is Function) {
    return string.replaceAllMapped(patternObj, (Match match) {
      return replacement(match);
    });
  } else {
    return string.replaceAll(patternObj, replacement.toString());
  }
}