So I need to remove escaped backslashes from a string (in my case, a path simply like “C:\Program Files (x86)\Microsoft Office\Office14\WINWORD.EXE\”).
To replace I’ve tried the following:
String openWith = "C:\\Program Files (x86)\\Microsoft Office\\Office14\\WINWORD.EXE";
string newString = openWith.Replace(@"\\", @"\");
openWith = openWith.Replace(@"\\", @"\");
openWith = Regex.Replace(openWith,"\\\\","\\");
But none of these work!! Would anyone be able to explain to me why this may be?
Thank you in advance!
Your string doesn’t contain doubled backslashes.
The first backslash in
"C:\\"is an escape character which is interpreted by the C# compiler. At runtime however the string only contains single backslashes. You can prove this to yourself by displaying the value of the string:Result:
Note that there are only single backslashes in the output.
See it working online: ideone