Replacing \n With \r using the Regex.Replace
Here is a question i received the other day from one of victims (Nick):
====
I've been watching the recordings of your webcasts on regex (just about to see no. 7) one query that I have is how to include metacharacters in the replacement string. So in a simple example I want to replace a linefeed with, for arguments sake, a carriage return.
if I do string myString1 = Regex.Replace(myString, "\n", "\r"); it finds the new line but it replaces it with a literal \r. The same holds true in Expresso.
After 25 years with Unix and vi I can do this so what am I missing in .Net ? Any help greatly appreciated.
====
There may be multiple ways to deal with this but the answer I came up with for this particular scenario was this:
String myString1 = Regex.Replace(myString,"\n",Convert.ToString(Convert.ToChar(13)));
It was a quick fix so, by all means, if you have a better or even cleaner way you think it could be written feel free to comment with your suggestion :)