Escape Double Quotes in C#
-
Escape Double Quotes With the
""
Escape Character inC#
-
Escape Double Quotes With the
\
Escape Character inC#

This tutorial will discuss methods to escape double quotes in C#.
Escape Double Quotes With the ""
Escape Character in C#
If we want to save a string like He said, "Hi"
, we have to use the double quotes escape character ""
in C#. We have to enclose the double quotes inside another pair of double quotes like ""Hi""
to store this inside a string variable like "Hi"
. The following code example shows us how we can escape double quotes with the ""
escape character in C#.
using System;
namespace escape_quotes
{
class Program
{
static void Main(string[] args)
{
string msg = @"He said ""Hi""";
Console.WriteLine(msg);
}
}
}
Output:
He said "Hi"
We saved the string msg
with the value He said "Hi"
by using the ""
escape character in C#.
Escape Double Quotes With the \
Escape Character in C#
We can also use the \
escape character to store the string He said "Hi"
inside a string variable in C#. We would have to write a \
before each double quote like He said \"Hi\"
. The following code example shows us how we can escape double quotes with the \
escape character in C#.
using System;
namespace escape_quotes
{
class Program
{
static void Main(string[] args)
{
string msg = "He said \"Hi\"";
Console.WriteLine(msg);
}
}
}
Output:
He said "Hi"
We saved the string msg
with the value He said "Hi"
by using the \
escape character in C#.
Maisam is a highly skilled and motivated Data Scientist. He has over 4 years of experience with Python programming language. He loves solving complex problems and sharing his results on the internet.
LinkedInRelated Article - Csharp String
- C# Convert String to Enum
- C# Convert Int to String
- Use Strings in Switch Statement in C#
- Convert a String to Boolean in C#
- Convert a String to Float in C#
- Convert a String to a Byte Array in C#