how to automatically escape the path
Asked Answered
G

4

9

I have a path string like c:\user\test\test.jpg, how can I make it c:\\user\\test\\test.jpg?

Gammadion answered 28/12, 2010 at 4:38 Comment(3)
you can not assign a string like string s="c:\user\test\test.jpg"; It will give compilation error, string can take only "\\" instead of "\", But string literal "\\" always treated same as "\".Lexicographer
@Asif, that is not correct. See my answer.Swarey
@AsifQadri, not only one can use the verbatim string syntax to define a string (the at sign), but also one could pass such a string into the arguments of a 'main' function.Emblazon
S
16
string s = s.Replace(@"\", @"\\");
Sumach answered 28/12, 2010 at 4:43 Comment(0)
S
28

Try this:

string path = @"c:\user\test\test.jpg";
Swarey answered 28/12, 2010 at 4:43 Comment(1)
how to do opposite of this? and when there are random no of \\ in the path for e.g. how can i make this path C:\\abcdef\\\\smstr\\iretrieval\\20_newsgroups\\20_newsgroups\\alt.atheism\\ as C:\abcdef\smstr\iretrieval\20_newsgroups\20_newsgroups\alt.atheism\?Communize
S
16
string s = s.Replace(@"\", @"\\");
Sumach answered 28/12, 2010 at 4:43 Comment(0)
V
5

you would only require escaping if you are using string literal in the code. why would you require automatic escaping anyways. you can use @ before the literal that requires no escaping.

Venegas answered 28/12, 2010 at 4:43 Comment(0)
C
0

You can always try something like: System.Text.RegularExpressions.Regex.Unescape, of course that will do all escaped characters.

Cormier answered 18/11, 2019 at 18:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.