C# 如何在字符串中写入反斜杠 (\)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18532691/
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 do I write a backslash (\) in a string?
提问by
I want to write something like this C:\Users\UserName\Documents\Tasks
in a textbox
:
我想C:\Users\UserName\Documents\Tasks
在 a 中写这样的东西textbox
:
txtPath.Text = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)+"\Tasks";
I get the error:
我收到错误:
Unrecognized escape sequence.
无法识别的转义序列。
How do I write a backslash in a string?
如何在字符串中写反斜杠?
采纳答案by Chris Sinclair
The backslash ("\"
) character is a special escape character used to indicate other special characters such as new lines (\n
), tabs (\t
), or quotation marks (\"
).
反斜杠 ( "\"
) 字符是一种特殊的转义字符,用于指示其他特殊字符,例如换行符 ( \n
)、制表符 ( \t
) 或引号 ( \"
)。
If you want to include a backslash character itself, you need two backslashes or use the @
verbatim string:
如果要包含反斜杠字符本身,则需要两个反斜杠或使用@
逐字字符串:
var s = "\Tasks";
// or
var s = @"\Tasks";
Read the MSDN documentation/C# Specificationwhich discusses the characters that are escaped using the backslash character and the use of the verbatim string literal.
阅读MSDN 文档/C# 规范,其中讨论了使用反斜杠字符转义的字符以及逐字字符串文字的使用。
Generallyspeaking, most C# .NET developers tend to favour using the @
verbatim strings when building file/folder paths since it saves them from having to write double backslashes all the time and they can directly copy/paste the path, so I would suggest that you get in the habit of doing the same.
一般来说,大多数 C# .NET 开发人员@
在构建文件/文件夹路径时倾向于使用逐字字符串,因为这样可以避免他们一直写双反斜杠,而且他们可以直接复制/粘贴路径,所以我建议你养成这样做的习惯。
That all said, in this case, I would actually recommend you use the Path.Combine
utility method as in @lordkain's answeras then you don't need to worry about whether backslashes are already included in the paths and accidentally doubling-up the slashes or omitting them altogether when combining parts of paths.
话虽如此,在这种情况下,我实际上建议您使用@lordkain 的回答中的Path.Combine
实用程序方法,因为这样您就不必担心反斜杠是否已包含在路径中,并且不小心将斜杠加倍或省略了它们合并部分路径时完全相同。
回答by Andre Lombaard
Just escape the "\"
by using + "\\Tasks"
or use a verbatim string like @"\Tasks"
只需"\"
使用 +"\\Tasks"
或使用逐字字符串来转义@"\Tasks"
回答by Kyle
To escape the backslash, simply use 2 of them, like this:
\\
要转义反斜杠,只需使用其中的 2 个,如下所示:
\\
回答by Michael Moreno
The previous answer is correct but in this specific case I would recommend using the System.IO.Path.Combinemethod.
前面的答案是正确的,但在这种特定情况下,我建议使用System.IO.Path.Combine方法。
You can find more details here: http://msdn.microsoft.com/en-us/library/fyy7a5kt.aspx
您可以在此处找到更多详细信息:http: //msdn.microsoft.com/en-us/library/fyy7a5kt.aspx
回答by Nil
txtPath.Text = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)+"\\Tasks";
Put a double backslash instead of a single backslash...
用双反斜杠代替单反斜杠...
回答by lordkain
There is a special function made for this Path.Combine()
有为此Path.Combine()制作的特殊功能
var folder = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
var fullpath = path.Combine(folder,"Tasks");
回答by ksereis
even though this post is quite old I tried something that worked for my case .
尽管这篇文章已经很老了,但我尝试了一些对我的案例有用的东西。
I wanted to create a string variable with the value below:
我想创建一个具有以下值的字符串变量:
21541_12_1_13\":null
so my approach was like that:
所以我的方法是这样的:
build the string using verbatim
string substring = @"21541_12_1_13\"":null";
and then remove the unwanted backslashes using Remove function
string newsubstring = substring.Remove(13, 1);
使用逐字逐字构建字符串
string substring = @"21541_12_1_13\"":null";
然后使用删除功能删除不需要的反斜杠
字符串 newsubstring = substring.Remove(13, 1);
Hope that helps. Cheers
希望有帮助。干杯