translate method

String translate(
  1. Map<int, int> table
)

Replaces each character using the given translation table.

The table maps Unicode code points (integers) to replacement code points. Characters not found in the table are left unchanged.

Example:

final text = 'hello';
final table = {104: 72, 101: 69}; // h→H, e→E
final result = text.translate(table);
// Returns: 'HEllo'

Implementation

String translate(Map<int, int> table) {
  var buffer = StringBuffer();

  for (var rune in runes) {
    buffer.writeCharCode(table.containsKey(rune) ? table[rune]! : rune);
  }

  return buffer.toString();
}