How can I do a replace on a string so that “(” becomes “)” and “)” becomes “(“?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
It would be tempting to use
.replace()but if you replace)with(and then(with)you’ll end up with all). Instead, iterate over the string and use a stringbuilder to build your string.I’m aware you could put something in there as a placeholder to swap against, but if it already existed in your string, you’d have a big problem.
Consider using “xxx” as your swap string. If your string was “abcx(yz)”, and you replace ( with xxx, you end up with “abcxxxyz)” Then you replace ) with ( so you have “abcxxxxyz(“. Then you replace xxx with ) so you have “abc)xyz(“. Certainly not cool!