Append a character by numeric position in a string

I have strings of known (though variable) lengths and I know of the numeric position inside of these strings where I want to replace something (or more specifically append to it). I saw the thread “Replace specific characters at a specific position in a string” but it doesn’t quite do what I’m looking to do.
For example I might have strings
AAABBBBREDCCCCDDD
EEEEFFFFBLUEGGGGHHH

And I know from another mapping that “RED” starts at the 8th position of the first string while “BLUE” starts at the 9th position of the second. I want to now add “old” before each of those colors so that my strings now look like:
AAAABBBBoldREDCCCCDDD
EEEEFFFFoldBLUEGGGGHHH

I presume I need to use “String Manipulation” for this, but I’m not sure how to tell it to insert something at a given position. I know the “substr” option in there will extract a string by position, but I want to insert something in to a given position. As shown above, the position might be “RED” or “BLUE”, or perhaps something else - I only know the numeric position.

Use replace() function where you replace RED by oldRED so on.

That works in a simple case.
I can also have a string where “RED” occurs more than once, but I only want to change a specific case of “RED” to “oldRED”. I can also have a case where both “RED” and “BLUE” occur, but I want to change “RED” to “oldRED” or “BLUE” to “oldBLUE” but not necessarily both in the same string; I have a numeric position that tells me which one should be appended.

Hi there!

Then In String Manipulation node use function join with substr like this this:
join(substr(string,0,your_position-1), "old", substr(string,your_position+length_of_color_name))
where length of color name you have to calculate for every row but if you know what color name you have to replace then you know its length as well :wink:

Hope this will help you.

Br,
Ivan

4 Likes

Excellent, thank you! That was exactly what I needed.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.