Main Question:
Is there a way to insert ONE double quotation in a string in C#
Information:
I am trying to perform something like the following SQL Select statement in C#
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"')"
I've also tried the way in this SOF question but it didn't work either!:
string selectCommand= @"select Name from Production.Products
where contains (doc.document_name,'"""""" + full + """"""')");
Quotations within a QuotationUse single quotation marks to enclose quotes within another quotation. The reporter told me, "When I interviewed the quarterback, he said they simply 'played a better game. '"
\" - escape sequence Since printf uses ""(double quotes) to identify starting and ending point of a message, we need to use \" escape sequence to print the double quotes.
In C and C++ the single quote is used to identify the single character, and double quotes are used for string literals. A string literal “x” is a string, it is containing character 'x' and a null terminator '\0'. So “x” is two-character array in this case.
Sometimes you might want to place quotation marks (" ") in a string of text. For example: She said, "You deserve a treat!" As an alternative, you can also use the Quote field as a constant.
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':
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 "
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With