C# 将文字“@”与字符串变量一起使用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/120763/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 14:30:56  来源:igfitidea点击:

Using the literal '@' with a string variable

提问by naspinski

I have a helper class pulling a string from an XML file. That string is a file path (so it has backslashes in it). I need to use that string as it is... How can I use it like I would with the literal command?

我有一个从 XML 文件中提取字符串的助手类。该字符串是一个文件路径(所以它有反斜杠)。我需要按原样使用该字符串...如何像使用文字命令一样使用它?

Instead of this:

取而代之的是:

string filePath = @"C:\somepath\file.txt";

I want to do this:

我想做这个:

string filePath = @helper.getFilePath(); //getFilePath returns a string

This isn't how I am actually using it; it is just to make what I mean a little clearer. Is there some sort of .ToLiteral() or something?

这不是我实际使用它的方式;这只是为了让我的意思更清楚一点。是否有某种 .ToLiteral() 之类的?

采纳答案by brock.holum

I don't think you have to worry about it if you already have the value. The @ operator is for when you're specifying the string (like in your first code snippet).

如果你已经拥有了价值,我认为你不必担心。@ 运算符用于指定字符串时(如在您的第一个代码片段中)。

What are you attempting to do with the path string that isn't working?

您试图对不起作用的路径字符串做什么?

回答by Michael Stum

I'm not sure if I understand. In your example: if helper.getFilePath()returns "c:\somepath\file.txt", there will be no problem, since the @is only needed if you are explicitely specifying a string with "".

我不确定我是否理解。在您的示例中:如果helper.getFilePath()返回"c:\somepath\file.txt",则没有问题,因为@只有在您明确指定带有“”的字符串时才需要。

When Functions talk to each other, you will always get the literal path. If the XML contains c:\somepath\file.txtand your function returns c:\somepath\file.txt, then string filePath will also contain c:\somepath\file.txtas a valid path.

当函数相互通信时,您将始终获得文字路径。如果 XML containsc:\somepath\file.txt并且您的函数返回c:\somepath\file.txt,则字符串 filePath 也将包含c:\somepath\file.txt为有效路径。

回答by Jim Burger

In C# the @ symbol combined with doubles quotes allows you to write escaped strings. E.g.

在 C# 中,@ 符号结合双引号允许您编写转义字符串。例如

print(@"c:\mydir\dont\have\to\escape\backslashes\etc");

If you dont use it then you need to use the escape character in your strings.

如果您不使用它,那么您需要在字符串中使用转义字符。

http://msdn.microsoft.com/en-us/library/aa691090(VS.71).aspx

http://msdn.microsoft.com/en-us/library/aa691090(VS.71).aspx

You dont need to specify it anywhere else in code. In fact doing so should cause a compiler error.

您不需要在代码中的其他任何地方指定它。事实上,这样做应该会导致编译器错误。

回答by Sander

You've got it backwards. The @-operator is for turning literals into strings, while keeping all funky characters. Your path is already a string - you don't need to do anything at all to it. Just lose the @.

你已经倒退了。@-operator 用于将文字转换为字符串,同时保留所有时髦的字符。你的路径已经是一个字符串——你根本不需要对它做任何事情。只是失去@。

string filePath = helper.getFilePath();

回答by Gary Willoughby

The string returned from your helper class is not a literal string so you don't need to use the '@' character to remove the behaviour of the backslashes.

从您的助手类返回的字符串不是文字字符串,因此您不需要使用“@”字符来删除反斜杠的行为。

回答by crashmstr

The @"" just makes it easier to write string literals.

@"" 只是使编写字符串文字更容易。

string (C# Reference, MSDN)

字符串(C# 参考,MSDN)

Verbatim string literals start with @ and are also enclosed in double quotation marks. For example:

@"good morning" // a string literal

The advantage of verbatim strings is that escape sequences are not processed, which makes it easy to write, for example, a fully qualified file name:

@"c:\Docs\Source\a.txt" // rather than "c:\\Docs\\Source\\a.txt"

逐字字符串文字以@ 开头,也用双引号括起来。例如:

@"good morning" // a string literal

逐字字符串的优点是不处理转义序列,这使得编写很容易,例如,完全限定的文件名:

@"c:\Docs\Source\a.txt" // rather than "c:\\Docs\\Source\\a.txt"

One place where I've used it is in a regex pattern:

我使用它的一个地方是正则表达式模式:

string pattern = @"\b[DdFf][0-9]+\b";

If you have a string in a variable, you do not need to make a "literal" out of it, since if it is well formed, it already has the correct contents.

如果变量中有一个字符串,则不需要从中生成“文字”,因为如果它的格式正确,则它已经具有正确的内容。