-1

enter image description here string sample="How to add doublequotes"; string str = '"'+sample+'"';

This is the output I am getting - "\"How to add doublequotes\"" I want the output as str= "How to add doublequotes" I am using vs code19

3 Answers3

1

The output that appears in your debugger is not the output you'll receive on screen.
When printing this out to [Console / API / Website], everything will work as expected.

To see what I mean, try creating a console application with the following code on Main:

static void Main(string[] args)
{
    string str = "\"This is with doublequotes\"";
    Console.WriteLine(str);
}  

BTW: You can just write string str = "\"How to add doublequotes\"" and it would still work.

TheSiIence
  • 327
  • 2
  • 10
  • I have to use this in the query string i.e something like this- Select * from tbl where Username=username suppose the value of username="abc" Now, in the query string, it goes as - Select * from tbl where username=abc What its should go as is Select * from tbl where username="abc" I hope you got my point! – Siddharth Kumar Shukla Dec 19 '20 at 13:59
  • 1
    @SiddharthKumarShukla the same applies for the query. if `string username = "abc"` then `Select * from tbl where username=\"" + username + "\""` – TheSiIence Dec 19 '20 at 14:47
  • @Administrator **Do not encourage SQL Injection** – mjwills Dec 19 '20 at 23:05
-1

If you use special characters or a newline in your text, you can use verbatim strings. They are denoted, by putting an @ before the string. Within a verbatim string, you don't need to escape special characters anymore, with one exception: the double quotes " need to be escaped using another double quotes:

string sample = @""How to add double quotes"";

When combining verbatim strings with interpolated strings, you ought to put the $ first:

string sample = $@"He said: ""Yes, for sure!"", but was only {p.Value} ÷ sure about it.";
Joki
  • 9
  • 1
  • 2
-1

Error Resolved by using this-

string query = "select * from customer where country = '" + USERNAME + "'";

Thanks everyone for investing your precious time!