# Text.Replace

**URL:** <https://forum.dynamobim.com/t/text-replace/56958>\
**Category:** DesignScript\
**Tags:** dynamo\
**Created:** [November 5, 2020, 5:01pm UTC](https://forum.dynamobim.com/t/text-replace/56958 "2020-11-05T17:01:26Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Congzheng](https://avatars.discourse-cdn.com/v4/letter/c/c0e974/32.png) [@Congzheng](https://forum.dynamobim.com/u/Congzheng)\
**Post date:** [November 5, 2020, 5:01pm UTC](https://forum.dynamobim.com/t/text-replace/56958/1 "2020-11-05T17:01:26Z")

</div>

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 ?

 ![StringReplace](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/d/6/d6509a641e860e618a138a0a4048803685379e28.png)

---

<div class="post-metadata">

**Author:** ![Nick\_Boyts](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/nick_boyts/32/76313_2.png) [@Nick\_Boyts](https://forum.dynamobim.com/u/Nick_Boyts)\
**Post date:** [November 5, 2020, 6:06pm UTC](https://forum.dynamobim.com/t/text-replace/56958/2 "2020-11-05T18:06:32Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![c.poupin](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/c.poupin/32/89152_2.png) [@c.poupin](https://forum.dynamobim.com/u/c.poupin)\
**Post date:** [November 5, 2020, 9:45pm UTC](https://forum.dynamobim.com/t/text-replace/56958/3 "2020-11-05T21:45:06Z")

</div>

Hello  
a solution with a python dictionary

 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/b/7/b700809c05c9b3458beded336a904b85fcde01ba.png)

```
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
```

---

<div class="post-metadata">

**Author:** ![Vikram\_Subbaiah](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/vikram_subbaiah/32/50797_2.png) [@Vikram\_Subbaiah](https://forum.dynamobim.com/u/Vikram_Subbaiah)\
**Post date:** [November 6, 2020, 4:47am UTC](https://forum.dynamobim.com/t/text-replace/56958/4 "2020-11-06T04:47:02Z")

</div>

A Dynamo Dictionary solution…

 ![replaceStrings_2020-11-06_10-15-20](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/1/8/182d1ee26d1c8978f3c6a4d30d9fd01da5bd53fe.png)  
[replaceStrings.dyn](https://forum.dynamobim.com/uploads/short-url/2sScsxcXwOrEIK5rxEz3SAhfhhi.dyn) (17.5 KB)

 ![replaceStrings_2020-11-06_10-22-09](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/8/9/8912bc088f9aed30be9e74d4073cff7da62256c9.png)

```
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);
```

---

<div class="post-metadata">

**Author:** ![Congzheng](https://avatars.discourse-cdn.com/v4/letter/c/c0e974/32.png) [@Congzheng](https://forum.dynamobim.com/u/Congzheng)\
**Post date:** [November 6, 2020, 8:34am UTC](https://forum.dynamobim.com/t/text-replace/56958/5 "2020-11-06T08:34:14Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Daan](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/daan/32/82246_2.png) [@Daan](https://forum.dynamobim.com/u/Daan)\
**Post date:** [November 6, 2020, 2:27pm UTC](https://forum.dynamobim.com/t/text-replace/56958/6 "2020-11-06T14:27:35Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ruben.romero](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/ruben.romero/32/115842_2.png) [@ruben.romero](https://forum.dynamobim.com/u/ruben.romero)\
**Post date:** [May 27, 2022, 1:48pm UTC](https://forum.dynamobim.com/t/text-replace/56958/8 "2022-05-27T13:48:58Z")

</div>

> [@c.poupin](#):
>
> a solution with a python dictionary

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

---

<div class="post-metadata">

**Author:** ![GavinNicholls](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/gavinnicholls/32/190442_2.png) [@GavinNicholls](https://forum.dynamobim.com/u/GavinNicholls)\
**Post date:** [May 27, 2022, 2:08pm UTC](https://forum.dynamobim.com/t/text-replace/56958/9 "2022-05-27T14:08:22Z")

</div>

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

> **[Python String replace() Method](https://www.w3schools.com/python/ref_string_replace.asp)**
>
> W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.

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

---

<div class="post-metadata">

**Author:** ![ruben.romero](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/ruben.romero/32/115842_2.png) [@ruben.romero](https://forum.dynamobim.com/u/ruben.romero)\
**Post date:** [May 27, 2022, 2:21pm UTC](https://forum.dynamobim.com/t/text-replace/56958/10 "2022-05-27T14:21:44Z")

</div>

> [@c.poupin](#):
>
> ```auto
> 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]),2))
> else:
> out.append(valueTxt)
> 	
> OUT = out
> 
> ```

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

---

<div class="post-metadata">

**Author:** ![c.poupin](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/c.poupin/32/89152_2.png) [@c.poupin](https://forum.dynamobim.com/u/c.poupin)\
**Post date:** [May 27, 2022, 6:13pm UTC](https://forum.dynamobim.com/t/text-replace/56958/11 "2022-05-27T18:13:07Z")

</div>

An example

 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/f/9/f9fb8cd9204ca3f45c4fbfac1e91de0120f3d47d.png)

---

<div class="post-metadata">

**Author:** ![ruben.romero](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/ruben.romero/32/115842_2.png) [@ruben.romero](https://forum.dynamobim.com/u/ruben.romero)\
**Post date:** [May 30, 2022, 8:07am UTC](https://forum.dynamobim.com/t/text-replace/56958/12 "2022-05-30T08:07:38Z")

</div>

> [@c.poupin](#):
>
> An example

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
