Text.Replace

Hallo, how do you replace a list of the text in Dynamo ?
I’ve tried many different ways to understand the logic of list “@L1, @L2, @L3” in Dynamo, but I can’t.
Would you give some advices ?

You can’t replace multiple substrings at once like that. You would have to replace each one individually or write something in Python.

2 Likes

Hello
a solution with a python dictionary

out = []

dictRep = dict(zip(IN[1], IN[2]))

for valueTxt in IN[0]:
	checkeyLst = [ _key for _key in dictRep.keys() if _key in valueTxt]
	if len(checkeyLst) > 0:
		out.append(valueTxt.replace(checkeyLst[0], dictRep.get(checkeyLst[0])))
	else:
		out.append(valueTxt)
	
OUT = out
6 Likes

A Dynamo Dictionary solution…


replaceStrings.dyn (17.5 KB)

a = List.SetDifference(lst1,lst2);
b = List.Flatten([lst2,a],-1);
c = List.Flatten([lst3,a],-1);
d = Dictionary.ByKeysValues(b,c);
e = d.ValueAtKey(lst1);
5 Likes

Yes, I made the replace for the first text error. But then I found a second one, the third, the fourth…Then I told myself, it’s not going to work in the future because I may have 20 or even more texts to replace.

the Clockwork Package has a node that does exactly this: String.ReplaceMultiple :slight_smile:

2 Likes

in my case it replaced part of the text but not all the searchs in the list for same item, so what happens if the search text there are 3 items and in the item to replace there are those 3 items to replace? in this case only one of the search list is replaced in the original string item

This method should replace all matched in a string unpess you provide the count argument which is optional.

You can just run each replacement through a loop over each string to replace many times.

so based on your useful link, it says it is possible to add a comma with a number in text replace function so, I modified the code adding",2" but I have to write again same replace function to replace everything I need

1 Like

An example

3 Likes

it works very well, love it, so smooth.

I was thinking if would be more convinient to have a code of function definition (def) so if that replacement function needs to be performed more times, then less code lines