xscape
xscape

Reputation: 3376

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

Upvotes: 9

Views: 28623

Answers (4)

Mark Kram
Mark Kram

Reputation: 5842

How about:

string temp = "\\h\\k";
temp = temp.Replace("\\\\", "\\");

Upvotes: -1

TalkingCode
TalkingCode

Reputation: 13567

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

That should work.

Upvotes: 7

Michael Rodrigues
Michael Rodrigues

Reputation: 5137

You need to escape the slashes each time:

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

Upvotes: 3

Emond
Emond

Reputation: 50692

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

string temp = @"\\h\\k";
temp = temp.Replace(@"\\", @"\");

Upvotes: 6

Related Questions