Ebrar Bayburt
Ebrar Bayburt

Reputation: 37

asp.net replace special character '\'

I have a string ,it gives a source path from a png image ex:

C:\pictures\picture1.png

But I will replace the '\' character to '\ \' (two \ character )with this line :

my data is C:\pictures\picture1.png

 public string rep(string data) 
        {
            data.Replace('\','\\');
            return data;
        }

when I rite data.Replace('\','\') the next characters after '\' comes to description line what can I do I must use the file source so :

C:\\pictures\\picture1.png

Upvotes: 0

Views: 13920

Answers (2)

Icarus
Icarus

Reputation: 63956

Your:

data.Replace("\","\\\\");

Line is not doing anything since Replace returns a new string with the result. Perhaps what you intend to do is data=data.Replace()...?

Upvotes: 2

RichieHindle
RichieHindle

Reputation: 281435

You need this:

data = data.Replace("\\", "\\\\");

Backslashes must be doubled in strings.

(Or use "raw strings":

data = data.Replace(@"\", @"\\");

}

Upvotes: 3

Related Questions