Regular expressions are possibly what you're after.
You can then use a regular expression with the the replace method to achieve the replacements that you're after. You can also use straight string replacement too.
Is this for a course that you're taking? Or is there some data-set that needs to be converted. If the latter, we should be able to help you find pre-existing solutions that already exist in the wild.