Q:

Replace substring several times with offset

For example i have a string:
"abcd abcd abcd"

And i need to replace first 'b' for 'ed' and third 'c' for 'ad'(not exactly in that order).
I know indexes of these letters from the very beginning.
The problem is when i replacing first 'b' the index of third 'c' is shifting to right. So the only way to solve this task is to store indexes of letters which i want to replace and then increase or decrease their indexes depending on where i made a replacement and length of replacement string.
Is there any library which can do this for me?
Update:
The order of replacement is specified by user so i don't know which letter and when will be replaced.
The source string is very large and it's takes too long to calculate replacement indexes.

A:

If you know the indexes of the letters you wish to replace, then you can simply sort the list by their indexes descending, then work backwards through your string - that way, the letter indexes wont be altered.

