iDecode
iDecode

Reputation: 28886

How to replace 2 string letters from a String in Dart?

var string = '12345';

I want to replace 2 and 4 with 0, so I tried:

var output = string.replaceAll('2, 4', '0') // doesn't work

I am not sure if I need to use RegExp for this small stuff? Is there any other way to solve this?

Upvotes: 1

Views: 145

Answers (2)

Josteve Adekanbi
Josteve Adekanbi

Reputation: 12673

This works

void main() {
   var string = '12345';
    string = string.replaceAll('2', "0");
    string = string.replaceAll('4', "0");
    print(string);
}

Upvotes: 1

Eiko
Eiko

Reputation: 25632

You can achieve this with RegExp:

final output = string.replaceAll(RegExp(r"[24]"),'0');

[24] matches any character that is either 2 or 4.

Upvotes: 2

Related Questions