String replace

Personally I don’t like dictionaries for string replacement, as they tend to fall apart a bit due to the in-controllable nature of strings. Case in point if you were to look for the key “000110” you’d want to return “SK1,” but since there is no exact match for “000110” in the dictionary you receive null instead. This is not only not the intended result, but it introduces a change in data type as well. Simply put, with strings I feel that there are often too many wildcards to utilize dictionaries effectively.

So that puts me back to the drawing board. Instead of chaining all the nodes together you can call the entire string replacement chain in one code block as @Ewan_Opie indicated (with some added steps to clarify):

.

Even better, if you have an unknown number of replacement pairs, or you don’t want to spend forever coding each possible replacement, you could utilize a custom iterative function. By iterating over the list for every input pair it changes the value sequentially. This way it doesn’t matter how many replacement terms you have - in the end you’ll get what you’re after:

There are a few issues with replacing the same text twice (note the space before and after " be " in my first pair - this was required to not replace the ‘be’ in “better”), but this should get you started anyway.

3 Likes