Replace string in Flutter

Ariel Mejia Dev
Ariel Mejia Dev
Published in
1 min readJun 3, 2019

In Dart we have a method named replaceAll() that allows you to cut a string of text, this method supports two parameters, the first one must be a string with the text section inside the text string that you want to replace and the second parameter the text that you want to replace, see an example:

'resume'.replaceAll('e', 'é'); // it returns the string   'résumé'

It is useful to work with strings and it can be assign to a variable:

newString = 'resume';
newString.replaceAll('e', 'é'); // it returns the string 'résumé'

But thats not all you can handle date objects and replace some piece of a date as string, with the use of the helper toString(), like this example.

// returns 2019-06-20 00:00:00.000 as dateTime object
DateTime eventDate = DateTime.now();
// returns 2019-06-20 as string
Text('${eventDate.toString().replaceAll(' 00:00:00.000', '')}')

And you can use a regex to search like a needle in a haystack:

‘resume’.replaceAll(RegExp(r’e’), ‘é’); // ‘résumé’

Those are some examples of how to handle a string replacement with Dart in Flutter, if you have comments please be welcome to tell me, happy coding!

--

--

Ariel Mejia Dev
Ariel Mejia Dev

Fullstack Web/Mobile Developer and Laravel enthusiast.