Dart replace all substring in a string example

Introduction :

Dart provides one method called replaceAllMapped to replace all substring in a string easily. In this blog post, we will learn how to use this method with an example.

Definition :

replaceAllMapped is defined as below :

String replaceAllMapped (
Pattern p,
String replace(
Match m
)
)

It takes one Match and replace all substrings that matched with the value computed from the Match. It replaces all these substrings and returns one new string.

This method can be used to find out complex and different types of substrings using a regex. This is better than the replaceAll method where only a specific substring can be passed.

Example :

import "dart:core";

void main() {
  final givenString = "abcdefghijklmnopqrstuvwxyz";
  final newString =
      givenString.replaceAllMapped(new RegExp(r'[aeiou]'), (match) {
    return '*';
  });
  print(newString);
}

In the above example, we are replacing all lowercase vowels in a string with *. It will print the below output :

*bcd*fgh*jklmn*pqrst*vwxyz

dart replace substring in string

Where is the color and why codevscolor ?

Long story short, I love paintings and I paint on weekends. We(me and my wife) have one Youtube channel. Don't forget to subscribe ❤️❤️🤩