-1

This might seem like a question that's been answered many times. My team and I have tried many solutions over the past hour without any luck. We have a database driven string value that contains c:\test and we want to replace the backslash with \\ resulting in c:\\test.

We've tried using .Replace, Regex.Replace, .Split and rebuilding the string, I tried using a for loop and substring to examine each character. When you get past the colon the next character shows up as "\t".

Please try the solution before submitting as we've tried a lot of different methods including dozens of suggestions already on stack overflow.

If we manually set the string as a literal like path = @"c:\test" then using replace works fine.

I would think that the solution would be to create a string that doesn't process the escape character but I have no idea how to implement that.

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
CubeRoot
  • 552
  • 2
  • 13
  • Have your tried "c:\\test"? – Kevin Raffay Nov 16 '16 at 22:09
  • In case this oddity happens to someone else. This only happened because the path of "C:\temp" was used. Had we defaulted the string to "c:\logs" then we would have been given an invalid escape but since \t is the tab as mentioned by Alexei then it accepted the string as valid but was pointing to "c: emp". – CubeRoot Dec 15 '16 at 21:35

1 Answers1

0

Sounds like your string already contains "tab" character ('\t') you probably need to replace it with "\\t" :

var result = "c:\test".Replace("\t", "\\t");
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
  • I noticed that if I set the value using args from a console application that it automatically escapes the string. You are right that the \t is treated as a tab. I'm a bit puzzled that it's being populated like that and not being converted to an escaped back slash. If you try changing c:\temp to c:\path then the string throws an error that \p is not a valid escape. – CubeRoot Nov 17 '16 at 00:05