tags:

views:

271

answers:

5

I'm sure this is something really basic that I don't know but how do I make it not recognize "\" as an escape sequence inside a string

I'm trying to type in a path and it thinks it is an escape sequence

+2  A: 

It's pretty simple, just do two slashes:

\\

Jeremy Morgan
...without making it an escape sequence...
mjv
From the Question:I'm trying to type in a path and it thinks it is an escape sequenceHe's trying to use a backlash in a path without C# seeing it as an escape sequence, so two slashes is his answer.
Jeremy Morgan
+7  A: 
string s = @"C:\Temp";
ChaosPandion
A: 

use "\\"

Funny thing: I had to escape \ using \\.

aramadia
+16  A: 

You can use Verbatim String Literals:

//Initialize with a regular string literal.
string oldPath = "c:\\Program Files\\Microsoft Visual Studio 8.0";

// Initialize with a verbatim string literal.
string newPath = @"c:\Program Files\Microsoft Visual Studio 9.0";
                 ↑
dtb
Nice arrow character.
Michael Petrotta
kudos on showing the differences!
Dr. Watson
Okay, how'd you do that?
Chris
+1 for naming it, not just giving it.
MPelletier
That is really lame... but hilarious at the same time.
ChaosPandion
I think it was the arrow...
ChaosPandion
A: 

It's Simple... Just put '@' symbol before your string, then it never care for your escape sequences...like this

string name=@"lndebi\n\nlndebi";

the output will be lndebi\n\nlndebi.

Jawahar BABU
*Never* care for your escape sequences... except for double quotes, which have to be doubled: `@""""` is a string with a single double quote. (what a mess I made with the use of double and single)
Martinho Fernandes