@(at) 登录文件路径/字符串 [英] @(at) sign in file path/string
问题描述
可能的重复:
.NET 字符串前面的 @ 是什么?一个>
我有以下代码:
new Attachment(Request.PhysicalApplicationPath + @"pdf" + pdfItem.Value)
@
符号有什么作用?
推荐答案
与文件路径无关.它改变了字符串的转义行为.
It has nothing to do with filepath. It changes the escaping behavior of strings.
在以 @
为前缀的字符串文字中,以 开头的转义序列被禁用.这对于文件路径很方便,因为
是路径分隔符,您不希望它开始转义序列.
In a string literal prefixed with @
the escape sequences starting with are disabled. This is convenient for filepaths since
is the path separator and you don't want it to start an escape sequence.
在普通字符串中,您必须将 转义为
\
,因此您的示例将类似于pdf\".但是由于它以 @
为前缀,因此唯一需要转义的字符是 "
(转义为 ""
)和 可以简单地出现.
In a normal string you would have to escape into
\
so your example would look like this "pdf\". But since it's prefixed with @
the only character that needs escaping is "
(which is escaped as ""
) and the can simply appear.
此功能对于包含 的字符串文字很方便,例如文件路径或正则表达式.
This feature is convenient for strings literals containing such as filepaths or regexes.
对于您的简单示例,增益不是那么大,但是您有完整路径 "C:\ABC\CDE\DEF"
然后 @"C:ABCCDEDEF"
看起来好多了.
For your simple example the gain isn't that big, but image you have a full path "C:\ABC\CDE\DEF"
then @"C:ABCCDEDEF"
looks a lot nicer.
对于正则表达式,这几乎是必须的.正则表达式通常包含几个 已经转义其他字符,如果您需要转义它们,通常会变得几乎不可读.
For regular expressions it's almost a must. A regex typically contains several escaping other characters already and often becomes almost unreadable if you need to escape them.
这篇关于@(at) 登录文件路径/字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!