C# 逐字转义字符串文字

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

Escaping verbatim string literals

c#stringescapingverbatim-string

提问by Brian Sweeney

I have the following string which won't compile:

我有以下无法编译的字符串:

String formLookupPull = @"SELECT value1, '"+tableName+"', '"+columnName+"' FROM lkpLookups WHERE ""table"" = '" + tableName + "' and ""field"" = '" + columnName + "';";

The offending sections are :

违规部分是:

""table"" =

and

""field"" = 

The compiler is getting all mixed up on the escape sequence. Can anyone see what's wrong?

编译器把所有的转义序列都搞混了。任何人都可以看到有什么问题吗?

采纳答案by Quintin Robinson

The problem is that not all the strings you are concatenating are verbatimstring literals, only the first portion of the concatenation is.

问题是并非您连接的所有字符串都是逐字字符串文字,只有连接的第一部分是。

In other words,

换句话说,

@"SELECT value1, '"

is the only verbatimliteral in the entire statement to build the final string.

是整个语句中用于构建最终字符串的唯一逐字文字。

You would need to add @ in front of the rest of your strings to make them all verbatim.

您需要在其余字符串前添加 @ 以使它们全部逐字逐句。

Which would make it look like:

这将使它看起来像:

String formLookupPull = @"SELECT value1, '"+tableName+ @"', '"+columnName+ @"' FROM lkpLookups WHERE ""table"" = '" + tableName + @"' and ""field"" = '" + columnName + @"';";

回答by Blorgbeard is out

You want to use \"to escape quotes, not "".

您想使用\"转义引号,而不是"".

Like this:

像这样:

.. FROM lkpLookups WHERE \"table\" = '" ..

Edit:

编辑:

Further explanation:

进一步解释:

You only have an @on the first of all the strings you're concatenating. In literal strings (with an @in front) you escape quotes with a double quote. In normal strings, it's slash-quote.

您只有@在要连接的所有字符串中的第一个上有一个。在文字字符串中(@前面有一个),你用双引号转义引号。在普通字符串中,它是斜杠引号。

Eg.

例如。

string s = @"this is a literal string with ""quotes"" in it, " 
         +  "and this is a normal string with \"quotes\" in it";

string t = @"two literal strings" + @", concatenated together.";

回答by MattJ

String formLookupPull = @"SELECT value1, '"+tableName+"', '"+columnName+"' FROM lkpLookups WHERE \"table\" = '" + tableName + "' and \"field\" = '" + columnName + "';";

I also trust that you are escaping these variables correctly before building this query :)

我也相信您在构建此查询之前正确转义了这些变量:)

回答by Kelsey

Well after your first end of quote, the @ symbol is no longer being used anyways so you are free to use the escape character. Try putting your "table" wrapped in '[' like [table] and [field] or escaping the " character with a \.

在您的第一个引号结束之后,@ 符号不再被使用,因此您可以自由使用转义字符。尝试将您的“表格”包裹在 '[' 中,例如 [table] 和 [field] 或用 \ 转义 " 字符。

String formLookupPull = @"SELECT value1, '" + tableName + "', '" + columnName + "' FROM lkpLookups WHERE [table] = '" + tableName + "' and [field] = '" + columnName + "';";

回答by Daniel LeCheminant

To address your title question...

要解决您的标题问题...

To escape the quote in a verbatim string literal, use the quote-escape-sequence ""(that's two quote characters)

要转义逐字字符串文字中的引号,请使用 quote-escape-sequence ""(即两个引号字符)

string a = @"He said ""Hi!""..."; // He said "Hi!"...

See MSDNfor more details on escaping, etc.

有关转义等的更多详细信息,请参阅MSDN

Note that in your posted code, the only verbatim string is the very first one (with the @before it). The subsequent strings are not verbatim, so the proper escape sequence would be \".

请注意,在您发布的代码中,唯一的逐字字符串是第一个字符串(@在它之前)。随后的字符串不是逐字逐句的,因此正确的转义序列应该是\".

You can make it look prettier with string.Format:

你可以让它看起来更漂亮string.Format

String formLookupPull = 
   string.Format(@"SELECT value1, '{0}', '{1}' FROM lkpLookups" +
                 @"WHERE ""table"" = '{0}' and ""field"" = '{1}';", 
                 tableName, columnName)

回答by CMS

If you cannot use SQL Parameters, String.Format can be little cleaner and readable than pure "+ concatenation".

如果您不能使用 SQL 参数,则 String.Format 可以比纯“+ 连接”更清晰和可读。

string formLookupPull = 
  string.Format(@"SELECT value1, '{0}', '{1}' 
                       FROM lkpLookups 
                   WHERE ""table"" = '{0}' AND ""field"" = '{1}';",
                tableName, columnName);

回答by Quintin Robinson

Why are you quoting the literal names of the columns, seem unnecessary to me.

你为什么要引用列的字面名称,对我来说似乎没有必要。

"SELECT value1, " + tableName + "," + columnName +" FROM lkpLookups WHERE table = '" + tableName + "' and field = '" = columnName + "';";

"SELECT value1," + tableName + "," + columnName +" FROM lkpLookups WHERE table = '" + tableName + "' and field = '" = columnName + "';";

Not tested but I think you will get the idea.

没有经过测试,但我想你会明白的。