Search code examples
flutterdartlibraries

How to insert characters into a Dart String?


I would like to add some white spaces to a Dart String in a given position, exactly like this (In Java).

so...

'XPTOXXSFXBAC' become 'XPTO XXSF XBAC'

Is there an easy way?


Solution

  • You can use the replaceAllMapped method from String, you have to add the regular expression, like this:

     final value =  "XPTOXXSFXBAC".replaceAllMapped(RegExp(r".{4}"), (match) => "${match.group(0)} ");
     print("value: $value");