How to use “\\” in a string without making it an e

2020-02-10 05:34发布

问题:

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

回答1:

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";
                 ↑


回答2:

string s = @"C:\Temp";


回答3:

use "\\"

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



回答4:

It's pretty simple, just do two slashes:

\\



回答5:

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.



回答6:

You can use " \\ " instead of " \ " as well as '@' sign in the beginning.



标签: c# escaping