Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C#: How to replace \\ with \

I have this string temp and I want to replace \\ with \

string temp = "\\h\\k";

I've tried doing temp.Replace("\\", "\") however the output is hk I want the output to be \h\k

How to replace "\\" with "\"?

Thank you

like image 583
xscape Avatar asked Apr 14 '11 04:04

xscape


3 Answers

temp.Replace("\\\\", "\\")

That should work.

like image 195
TalkingCode Avatar answered Nov 18 '22 09:11

TalkingCode


the question isn't quite clear, are you looking for this?

string temp = @"\\h\\k";
temp = temp.Replace(@"\\", @"\");
like image 34
Emond Avatar answered Nov 18 '22 10:11

Emond


You need to escape the slashes each time:

temp.Replace("\\\\", "\\")
like image 43
Michael Rodrigues Avatar answered Nov 18 '22 11:11

Michael Rodrigues