为什么 .NET 会在路径中已经存在的斜杠上添加一个额外的斜杠? [英] Why does .NET add an additional slash to the already existent slashes in a path?
问题描述
我注意到 C# 向路径添加了额外的斜线 ().考虑路径
C:Test
.当我在文本可视化工具中检查带有此路径的字符串时,实际字符串是 C:\Test
.
I've noticed that C# adds additional slashes () to paths. Consider the path
C:Test
. When I inspect the string with this path in the text visualiser, the actual string is C:\Test
.
这是为什么?这让我很困惑,因为有时我可能想拆分路径(使用 string.Split()
),但不得不想知道要使用哪个字符串(一个或两个斜线).
Why is this? It confuses me, as sometimes I may want to split the path up (using string.Split()
), but have to wonder which string to use (one or two slashes).
推荐答案
.Net 没有在此处向您的字符串添加任何内容.您所看到的是调试器选择显示字符串的方式的影响.C#字符串可以用2种形式表示
.Net is not adding anything to your string here. What your seeing is an effect of how the debugger chooses to display strings. C# strings can be represented in 2 forms
- 逐字字符串:以
@
符号为前缀,无需转义\
字符 - 普通字符串:标准 C 样式字符串,其中
\
字符需要自己转义
- Verbatim Strings: Prefixed with an
@
sign and removes the need o escape\
characters - Normal Strings: Standard C style strings where
\
characters need to escape themselves
调试器会将字符串文字显示为普通字符串与逐字字符串.不过,这只是显示问题,不影响其潜在价值.
The debugger will display a string literal as a normal string vs. a verbatim string. It's just an issue of display though, it doesn't affect it's underlying value.
这篇关于为什么 .NET 会在路径中已经存在的斜杠上添加一个额外的斜杠?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!