String.Replace with lists

I can’t figure out how to use String.Replace with lists.
I want to replace strings “AA” with “00”, “BB” with “01” and “CC” with “02”. But I only get to change some of them.
See picture.

String.Replace_lists.dyn (13.6 KB)

Another option:

Edit - and if you need the same order:

Edit2 - not sure why it doesn’t return a single value, but I guess it could be nice to know which replacement value was used…

def replaceAll(item,searches,replacements)
{
return = [Imperative]
{
search = DSCore.List.FilterByBoolMask(searches,DSCore.String.Contains(item,searches))["in"][0];
replace = DSCore.List.FilterByBoolMask(replacements,DSCore.String.Contains(item,searches))["in"][0];
new = DSCore.String.Replace(item,search,replace);
}
return = new;
};
4 Likes

Thank you guys, these are all good solutions! Personally for now I prefer using nodes rather than all these codes, because it is easier to understand if something needs to be tweaked :slight_smile: thanks.

Hello Nick
the node If Equal return index from wich package is it?

It’s its own package.