Strip away portions of text designated with delimiters

Hi All,

I’m new here and have a lot of learning to do. I’m trying something that seems trivial to me which is, of course, very difficult in Dynamo. Sorry to paste a screen shot but, since I’m new, I’m not allowed to upload! Does anyone know how long I’ll have to wait for that?

What I’m trying to do is write a custom node that will strip away portions of text designated with delimiters so that, “My string is short” becomes “My is short” (no need to fill in that blank please :wink:).

The code that I have works great IF the input contains a designated string, but if it doesn’t it fails. I can catch these cases but I can’t find a mechanism to return the un-altered input string. How is this normally done? It seem like I must use Python pre-emptively return alternative output? How would that work?

String.Replace will do exactly this. Just replace the delimiter with an empty string ("") and you’re set.

1 Like

There has been a misunderstanding. I wrote the word “string” with and asterix on each side, but this forum interface automatically replaced my asterixis with italic formatting. This time I’m using the ampersand as the delimiter.

“My %string% is short” becomes “My is short”

My problem is that I don’t know what the string between the ampersands is going to be, so I can’t user String.Replace.

Gotcha. That makes sense. If each string is only going to contain a single substring to be removed then you can use String.Split to split at the delimiters. This will give you a list of 0) the first substring, 1) the substring to be removed, and 2) the final substring. You can then remove the middle substring and join your list back together.

The same logic works if you have multiple sets of delimiters, you just have to remove the substrings at alternating indices and concatenate your whole list back together.

EDIT: It actually seems like the logic between a single removal and multiple ends up being basically the same anyway. Just create a sequence of all even indices available in each split string and concatenate them back together. Then clean up any double spaces if you need to.

Hi @funkitect & @Nick_Boyts ,

What about a regular expression?

3 Likes

Thanks for pointing me that way. I tend to avoid regex because I can’t wrap my head around all the arcane syntax. However, it works perfectly! Thank you.

James

1 Like