replace method

String? replace(
  1. String text,
  2. String fromSymbol,
  3. String toSymbol
)

Replace an emoji by another emoji.

For example: replace('I ❤️ coffee', '❤️', '❤️‍🔥') => 'I ❤️‍🔥 coffee'

Implementation

String? replace(String text, String fromSymbol, String toSymbol) {
  if (text.isEmpty) return null;

  final buffer = StringBuffer();
  for (final character in text.characters) {
    if (character == fromSymbol) {
      buffer.write(toSymbol);
    } else {
      buffer.write(character);
    }
  }
  return buffer.toString();
}