@(at)登录文件路径/字符串 [英] @(at) sign in file path/string

查看:152
本文介绍了@(at)登录文件路径/字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

在.NET的字符串前面的@是什么?

我有以下代码:

new Attachment(Request.PhysicalApplicationPath + @"pdf\" + pdfItem.Value)

@ sign do?

推荐答案

它与filepath无关。它改变了字符串的转义行为。

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:\ABC\CDE\DEF看起来更好。

For your simple example the gain isn't that big, but image you have a full path "C:\\ABC\\CDE\\DEF" then @"C:\ABC\CDE\DEF" 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆