How to replace the charactors in the string?

You never told us that performance is a requirement.