4
string value = "SELECT dbo.GetDefaultType(\"PartnerType\") AS default_answer;"

How can i remove "\" from the above string.

Get the ref from here and tried,

value.Replace(@"\", "");
value.Replace(@"\", string.Empty);
Smit Patel
  • 2,992
  • 1
  • 26
  • 44
  • 5
    The backslash is not there in the string value. It is only written in C# to tell the compiler that the following double quote belongs to the string value. – Joop Eggen Mar 29 '18 at 09:14
  • 1
    Look at the loupe in the debugger to see the real string which is: `SELECT dbo.GetDefaultType("PartnerType") AS default_answer;` – Tim Schmelter Mar 29 '18 at 09:14
  • \ is the escape char, not the backslash char – Jimbot Mar 29 '18 at 09:15

2 Answers2

5

The \ isn't actually in the string it is only there to stop the double quotes from terminating the string literal early.

The string actually is SELECT dbo.GetDefaultType("PartnerType") AS default_answer;

value could just as easily been declared as

string value = @"SELECT dbo.GetDefaultType(""PartnerType"") AS default_answer;"

Where "" inside the string is still only a single quote "

phuzi
  • 12,078
  • 3
  • 26
  • 50
  • 1
    @NithinChandran We'll probably never know ... Cheer up, phuzi. Sometimes ppl dv because their significant other had a mood in the morning ... – Fildor Mar 29 '18 at 09:24
  • @Fildor don't worry about me, it's just not constructive to down-vote without leaving a comment as to why – phuzi Mar 29 '18 at 09:55
3

I'm pretty sure that you just see the value in the debugger and it shows the \ from the string literal. If you click at the loupe you would see the real string value which is:

SELECT dbo.GetDefaultType("PartnerType") AS default_answer; 

But to answer the question, if the string really was(including the declaration)

string value = "SELECT dbo.GetDefaultType(\"PartnerType\") AS default_answer;"

Then it could be initalized with this literal:

string value  = "string value = \"SELECT dbo.GetDefaultType(\\\"PartnerType\\\") AS default_answer;\"";

and you could really remove the backslashes with:

value = value.Replace("\\", "");
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939