Modifying the Path
-
hello guys...I hae path stored in database which I retrieve successfuly. But before utilising them, I need to modify them. All of them have single backslah but of course, in order to use that path we need double backslash like this "\\". Here is what I have tried so far but it does not work of course. How can I do that?
string path = "d:\\songs\\audio\\asd.mp3"; string modifiedName = name.Replace("\\", "\\\\");
-
hello guys...I hae path stored in database which I retrieve successfuly. But before utilising them, I need to modify them. All of them have single backslah but of course, in order to use that path we need double backslash like this "\\". Here is what I have tried so far but it does not work of course. How can I do that?
string path = "d:\\songs\\audio\\asd.mp3"; string modifiedName = name.Replace("\\", "\\\\");
overloaded Name wrote:
of course ... we need double backslash
No you don't. Backslash doubling is meaningful only to the compiler itself, it is how one says the next backslash is to be taken literally; once a string literal is compiled, all escape sequences have been interpreted and no double backslashes remain (unless you had 4 backslashes, which could be useful in a UNC). FWIW: Your code, corrected so it compiles, but totally useless, would be:
string path = "d:\\\\songs\\\\audio\\\\asd.mp3"; // FIXED string modifiedName = path.Replace("\\\\", "\\\\\\\\"); // this says: replace 1 backslash by 2 backslashes!
:)
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
modified on Thursday, March 3, 2011 8:06 AM
-
overloaded Name wrote:
of course ... we need double backslash
No you don't. Backslash doubling is meaningful only to the compiler itself, it is how one says the next backslash is to be taken literally; once a string literal is compiled, all escape sequences have been interpreted and no double backslashes remain (unless you had 4 backslashes, which could be useful in a UNC). FWIW: Your code, corrected so it compiles, but totally useless, would be:
string path = "d:\\\\songs\\\\audio\\\\asd.mp3"; // FIXED string modifiedName = path.Replace("\\\\", "\\\\\\\\"); // this says: replace 1 backslash by 2 backslashes!
:)
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
modified on Thursday, March 3, 2011 8:06 AM
-
Hi Luc, Im sure you know that :), but the first line doesnt compile. Its an invalid string literal. One have to use
strind path = @"d:\songs\audio\asd.mp3";
or
string path = "d:\\songs\\audio\\asd.mp3";
Greets Matthias
My mistake, I copied and only fixed the second line. I'll fix it now. Thanks. :)
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.