`for` loop with dynamic condition

The replace() method that you're using here will copy the entire tail of the string on each replacement. You waste much more time on unnecessary character copies that you save by not allocating a single iterator object.

A much more efficient way to implement this method is to use two indexes into the StringBuilder, one for source and one for destination, and to copy the characters manually instead of using the replace() function. The source index will iterate over all the characters in the original StringBuilder, so you can use a regular for loop with a range, and you don’t need to update the range dynamically.