什么时候在字符串之前使用 @ 更好?

发布于 2024-11-04 06:44:53 字数 334 浏览 0 评论 0原文

在声明或使用 string 的代码中,我通常看到开发人员这样声明它:

string randomString = @"C:\Random\RandomFolder\ThisFile.xml";

而不是:

string randomString = "C:\\Random\\RandomFolder\\ThisFile.xml";

这是我认为使用 @ 更好的唯一方法前缀,因为您不需要执行 \\ ,但是当它比没有它更好时,它还有其他用途吗?

In code that declares or uses a string, I usually see the developers declare it like this:

string randomString = @"C:\Random\RandomFolder\ThisFile.xml";

Instead of:

string randomString = "C:\\Random\\RandomFolder\\ThisFile.xml";

That's the only thing that I see which is better to use the @ prefix, since you don't need to do \\, but is there any other use for it when it's better than just without it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

做个ˇ局外人 2024-11-11 06:44:53

@ 符号向编译器指示该字符串是 逐字字符串文字,因此不需要您转义任何字符。当然,不仅仅是反斜杠。编译器不处理任何类型的转义序列。

是否“更好”是一个极难回答的问题。这纯粹是风格上的选择。有些人可能会认为,当您使用字符串文字时,字符串内容更具可读性,而不必转义所有字符。其他人可能更喜欢一致性,其中包含通常需要转义的字符的所有字符串都必须进行转义。这使得您更容易一目了然地发现代码中的错误。 (就其价值而言,我属于后一个阵营。我的所有路径都有 \\。)

话虽如此,对于正则表达式来说,它极其方便,您可以使用正则表达式。否则就得到处逃亡了。而且由于它们看起来不太像常规字符串,因此混淆的风险很小。

The @ sign indicates to the compiler that the string is a verbatim string literal, and thus does not require you to escape any of the characters. Not just the backslash, of course. No escape sequences of any kind are processed by the compiler.

Whether it's "better" or not is an extremely difficult question to answer. This is a purely stylistic choice. Some might argue that the string contents are more readable when you use a string literal, rather than having to escape all of the characters. Others might prefer consistency, where all strings that contain characters that would ordinarily require escaping would have to be escaped. This makes it easier to notice errors in code at a glance. (For what it's worth, I fall into the latter camp. All my paths have \\.)

That being said, it's extremely convenient for regular expressions, for which you'd otherwise be escaping all over the place. And since they don't look much like regular strings, there's minimal risk of confusion.

欢烬 2024-11-11 06:44:53

Windows 路径名并不是唯一带有大量反斜杠的名称。例如,@字符串对于正则表达式非常有用,因为它们避免了必须对所有内容进行双重转义。

它们还可以跨越多行,因此如果您最终必须在代码中包含多行字符串,它们会变得更加方便。

Windows path names aren't the only things with a lot of backslashes. For instance, @-strings are very useful for regular expressions because they avoid having to double-escape everything.

They can also span multiple lines, so if you ever end up having to have multi-line strings in your code, they make it a bit more convenient.

当梦初醒 2024-11-11 06:44:53

使正则表达式更简单

@"^(([\w-]+\.)+[\w-]+|([a-zA-Z]{1}|[\w-]{2,}))((([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\.([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\.([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\.([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])){1}|([a-zA-Z]+[\w-]+\.)+[a-zA-Z]{2,4})$";

多行字符串

string s = @"testing
some

string with multiple
lines";

Makes Regex simpler

@"^(([\w-]+\.)+[\w-]+|([a-zA-Z]{1}|[\w-]{2,}))((([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\.([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\.([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\.([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])){1}|([a-zA-Z]+[\w-]+\.)+[a-zA-Z]{2,4})$";

Multiple lines string

string s = @"testing
some

string with multiple
lines";
小帐篷 2024-11-11 06:44:53

它对于涉及显式匹配反斜杠字符的正则表达式特别有用。由于这是 C# 字符串语法和正则表达式语法中的特殊字符,因此需要“双重转义”。示例:

string regex = "\\\\.*\\.jpg"

使用@符号的相同表达式会更整洁:

string regex = @"\\.*\.jpg"

It is especially useful for regular expressions that involve matching a backslash character explicitly. Since this is a special character in both C# strings syntax and regex syntax, it requires "double escaping". Example:

string regex = "\\\\.*\\.jpg"

Same expression using the @-notation would be more tidy:

string regex = @"\\.*\.jpg"
战皆罪 2024-11-11 06:44:53

"" 和 @"" 都是字符串文字,第一个是常规文字,但后者是逐字字符串文字
' C# .NET 中任何字符串之前的 @' 前缀(C# .NET 中的常规字符串文字和逐字字符串文字)

"" and @"" are both string literals, first is regular literal but the latter is a verbatim string literal
'@' prefix before any string in C# .NET (Regular string literal and Verbatim string literal in C#.NET)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文