String.Replace with lists


#1

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)


#2

like this?
String.Replace_lists.dyn (8.0 KB)


#3

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

If we are battling about the shortest…
String.Replace_lists2.dyn (3.0 KB)

replaced_str = String.Replace(input_str, search@-1<1>, replace@-1<1>);
elem = ((input_str != replaced_str@-2<1> ? replaced_str : null));
DSCore.List.Clean(elem, false);

#5

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.


#6

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


#7

It’s its own package.