Tuesday 10 September 2019

c# - How do I escape " in verbatim string?





I am a bit new to c#, and i am stuck at this point,
I have a regular string, where i made use of \ to escape ", escape here means that to escape the compilers interpretation of ", and get " printed on the screen, and i get the expected output-->



class Program
{
static void Main(string[] args)
{
string s1 = "This is a \"regular\" string";
System.Console.WriteLine(s1);
System.Console.Read();

}
}


o/p--> This is a "regular" string



Now, i have a verbatim string, and i am trying to escape " using \ in the same manner as above..-->



class Program
{

static void Main(string[] args)
{
string s2 = @"This is \t a \"verbatim\" string";//this would escape \t
System.Console.WriteLine(s2);
System.Console.Read();
}
}


Why the above isn't working ?



Answer



Use a double quote:



 string s2 = @"This is \t a ""verbatim"" string";

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...