Search code examples
c#stringquotes

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#

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 + """"""')");

Solution

  • 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 "