[英]How to deal with quotes within quotes in C#?
Main Question: 主要问题:
Is there a way to insert ONE double quotation in a string in C# 有没有办法在C#中的字符串中插入一个双引号
Information: 信息:
I am trying to perform something like the following SQL Select statement in C# 我试图在C#中执行类似下面的SQL Select语句
SELECT Name FROM Production.Product
WHERE CONTAINS(Name, '"chain*"');
I interpreted it this as follows: 我把它解释如下:
string selectCommand= @"select Name from Production.Products
WHERE contains (Name,'\"" + chain+ "*\"')");
But i'm getting the string back as: 但我得到的字符串:
" WHERE contains (Name,'\\"chain\\"')" “WHERE包含(名称,'\\”链\\“')”
I've also tried the way in this SOF question but it didn't work either!: 我也尝试过这个SOF 问题,但它也没有用!:
string selectCommand= @"select Name from Production.Products
where contains (doc.document_name,'"""""" + full + """"""')");
If you look at the documentation for string literals you will find that there are two formats. 如果查看字符串文字的文档,您会发现有两种格式。 The regular format where you have to escape all teh following characters: 常规格式,您必须转义所有以下字符:
', ", \, 0, a, b, f, n, r, t, u, U, x, v.
So your string should be written as follows: 所以你的字符串应该写成如下:
string query = "WHERE CONTAINS(Name, \'\"chain*\"\');";
or verbatim literals, which are preceded with an 'at' symbol, where you only escape double quotes by 'doubling up': 或逐字文字,前面带有'at'符号,您只能通过'加倍'来转义双引号:
string query = @"WHERE CONTAINS(Name, '""chain*""');";
So just drop the '@' from your string and it will work. 所以只需从字符串中删除“@”即可。
Its because you have a @
at the start of your string 这是因为你的字符串开头有一个@
string selectCommand= "select Name from Production.Products where contains (doc.document_name,\'\"" + full + "*\"\');";
You basically need to escape the '
and "
你基本上需要逃避'
和"
Simply drop the @ at the beginning of your string and it should work. 只需在字符串的开头删除@即可。
The reason why the backslash is preserved in your string is because you've told the string not to replace escaped character sequences by placing the @ in front of the string. 在字符串中保留反斜杠的原因是因为您已经告诉字符串不要通过将@放在字符串前面来替换转义的字符序列。 Removing the @ will allow your escaped characters to be, well, escaped. 删除@将允许您的转义字符转义。
Something like this maybe: 这样的事情可能是:
string full = "abc";
string selectCommand= @"select Name from Production.Products where contains (doc.document_name,'""" + full + "*\"')";
The "@" won't help you here. “@”对你没有帮助。 Remove it, and you should be fine. 删除它,你应该没事。 This should work: 这应该工作:
Console.WriteLine("...(doc.document_name,'\\"" + full + "*\\"')");
If you really want to keep the "@", try this: 如果你真的想保留“@”,试试这个:
Console.WriteLine(@"...(doc.document_name,'""" + full + "*\\"')");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.