Reputation: 5004
I have the following string:
"\\\\AAA.AA.A.AA\\d$\\ivr\\vm\\2012May\\29\\10231_1723221348.vox"
I would like it to look like:
"\\AAA.AA.A.AA\d$\ivr\vm\2012May\29\10231_1723221348.vox"
I have tried
fileToConvert.Replace(@"\\",@"\")
This produces:
"\\AAA.AA.A.AA\\d$\\ivr\\vm\\2012May\\29\\10231_1723221348.vox"
Why?!?
Thanks all!
Upvotes: 0
Views: 498
Reputation: 4340
I guess you look in the debugger and thats why you get this behaviour...
each \\
is actually one \
. thats why you get the \\\\
replaced to \\
(two "\" replaced by one)
and because \\
is actually only one "\" you still gets "\" after the replacement (because it didn't find two "\" string
the reason is that the \ character marks a special character, for example if you want to have tab (\t) character you will have the string "\t"
for new line "\r\n"
.
so when you actually want to have a '\' character in a string you mark it with one more '\' char before - like "\\"
that mean that when you see "\\AAA.AA.A.AA\d$\ivr\vm\2012May\29\10231_1723221348.vox" in the debugger, the actual string is "\AAA.AA.A.AA\d$\ivr\vm\2012May\29\10231_1723221348.vox"
so fileToConvert.Replace(@"\\",@"\")
will make it look like this:
"\AAA.AA.A.AA\d$\ivr\vm\2012May\29\10231_1723221348.vox"
which you will see in the debugger as "\\AAA.AA.A.AA\\d$\\ivr\\vm\\2012May\\29\\10231_1723221348.vox"
For a conclusion:
You don't need to do anything - not even fileToConvert.Replace(@"\\",@"\")
because your original string (what you see in debug as "\\\\AAA.AA.A.AA\\d$\\ivr\\vm\\2012May\\29\\10231_1723221348.vox"
) is actually "\\AAA.AA.A.AA\d$\ivr\vm\2012May\29\10231_1723221348.vox"
Upvotes: 2
Reputation: 12942
I'm guessing you are inspecting the result string in the debugger. This string will be escaped, i.e. TAB is \t and return is \n. The "real" value can be inspected by clicking the magnifying glass icon next to the value.
Also, doing a print to terminal (e.g. System.Console.WriteLine()
) will show the "correct" value.
Upvotes: 4
Reputation: 6612
string s = @"\\\\AAA.AA.A.AA\\d$\\ivr\\vm\\2012May\\29\\10231_1723221348.vox";
var output = s.Replace(@"\\",@"\");
I tried above code code and it works fine.
Upvotes: 0