I have a situation where I would like to rename a string as follows: Say I have a string x = "foo$" I want to rename this string as "foo\$" Unfortunately when I do this: x.replace("$", "\$") The new string becomes foo\\$ I need the literal to be recognized as a part of the pattern. I tried x.replace("$", "\\\$") but this gave the same result. How to fix this? Continue reading...