Reputation: 313
I am trying trying to add double quotes around a string but when ever I do it removes the first \
from the string.
I know I can add double quotes directly inside the string but I want to know why this method is not working.
Code
string SFilename = "\\FilePath";
SFilename = "\"" + SFilename + "\"";
Console.WriteLine(SFilename);
Output
"\FilePath"
Upvotes: 2
Views: 136
Reputation: 2433
\
is an escape sequence. You can either use \\
to add a single \
or surround the string with @
to declare it as verbatim.
string SFilename = @"\\FilePath";
SFilename = "\"" + SFilename + "\"";
Console.WriteLine(SFilename);
Upvotes: 2
Reputation: 39
You could use a const string and @:
const string quote = @"""";
string SFilename = @"\\FilePath";
SFilename = quote + SFilename + quote;
Console.WriteLine(SFilename);
Upvotes: 1
Reputation: 607
Thats because the you escape on of your \
in the original string.
You need to write:
string SFilename = "\\\\FilePath";
or you could simply use this (would result in the same thing):
string SFilename = @"\\FilePath";
Upvotes: 3