C# 如何将转义字符 '\' 写入代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15748040/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 17:50:12  来源:igfitidea点击:

How do I write the escape char '\' to code

c#escaping

提问by Stav Alfi

How to escape the character \in C#?

如何\在 C# 中转义字符?

采纳答案by Jon Skeet

You just need to escape it:

你只需要逃避它:

char c = '\';

Or you could use the Unicode escape sequence:

或者您可以使用 Unicode 转义序列:

char c = '\u005c';

See my article on stringsfor all the various escape sequences available in string/character literals.

有关字符串/字符文字中可用的所有各种转义序列,请参阅我关于字符串的文章

回答by Dustin Kingen

You can escape a backslash using a backslash.

您可以使用反斜杠转义反斜杠。

//String
string backslash = "\";

//Character
char backslash = '\';

or

或者

You can use the string literal.

您可以使用字符串文字。

string backslash = @"\";
char backslash = @"\"[0];

回答by Prakash Chennupati

use double backlash like so "\"

像这样使用双反冲“\”

"\"

cause an escape

逃跑

回答by Umar Farooq Khawaja

If you want to output it in a string, you can write "\\"or as a character, you can write '\\'.

如果你想输出成字符串,可以写"\\"或者作为字符,可以写'\\'.

回答by Justin

Double escape it. Escape escape = no escape! \\

双逃吧。逃脱逃脱=没有逃脱!\\

回答by Dave Clausen

Escape it: "\\"

逃脱它: "\\"

or use the verbatim syntax: @"\"

或使用逐字语法: @"\"