如何在字符串中使用“\”而不使其成为转义序列 - C#?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1768023/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use "\" in a string without making it an escape sequence - C#?
提问by Jarred Sumner
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
我正在尝试输入路径,但它认为这是一个转义序列
采纳答案by dtb
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";
↑
回答by Jeremy Morgan
It's pretty simple, just do two slashes:
这很简单,只需做两个斜线:
\\
\\
回答by ChaosPandion
string s = @"C:\Temp";
回答by aramadia
use "\\"
用 ”\\”
Funny thing: I had to escape \ using \\.
有趣的是:我不得不使用 \\ 来逃避 \。
回答by Jawahar BABU
It's Simple... Just put '@' symbol before your string, then it never care for your escape sequences...like this
这很简单......只需在你的字符串之前放置'@'符号,然后它就不会关心你的转义序列......就像这样
string name=@"lndebi\n\nlndebi";
字符串名称=@"lndebi\n\nlndebi";
the output will be lndebi\n\nlndebi.
输出将是 lndebi\n\nlndebi。
回答by Ravinath
You can use " \\ " instead of " \ " as well as '@' sign in the beginning.
您可以在开头使用“\\”代替“\”以及“@”符号。