How can I add double quotes to a string that is inside a variable?

C#asp.netDouble Quotes

C# Problem Overview


I have variable like:

string title = string.empty;

My need is that whatever string is passed to it I have to display the content inside a div with in a double quotes. So I have written something like:

...
...
<div>"+ title +@"</div>
...
...

But how can I add the double quotes here? So that it will display like:

"How to add double quotes"

C# Solutions


Solution 1 - C#

You need to escape them by doubling them (verbatim string literal):

string str = @"""How to add doublequotes""";

Or with a normal string literal you escape them with a \:

string str = "\"How to add doublequotes\"";

Solution 2 - C#

So you are essentially asking how to store doublequotes within a string variable? Two solutions for that:

var string1 = @"""inside quotes""";
var string2 = "\"inside quotes\"";

In order to perhaps make it a bit more clear what happens:

var string1 = @"before ""inside"" after";
var string2 = "before \"inside\" after";

Solution 3 - C#

If you have to do this often and you would like this to be cleaner in code, you might like to have an extension method for this.

This is really obvious code, but still I think it can be useful to grab it and make you save time.

  /// <summary>
    /// Put a string between double quotes.
    /// </summary>
    /// <param name="value">Value to be put between double quotes ex: foo</param>
    /// <returns>double quoted string ex: "foo"</returns>
    public static string AddDoubleQuotes(this string value)
    {
        return "\"" + value + "\"";
    }

Then you may call foo.AddDoubleQuotes() or "foo".AddDoubleQuotes(), on every string you like.

Solution 4 - C#

If I understand your question properly, maybe you can try this:

string title = string.Format("<div>\"{0}\"</div>", "some text");

Solution 5 - C#

You can also include the double quotes into single quotes.

string str = '"' + "How to add doublequotes" + '"';

Solution 6 - C#

Another note:

    string path = @"H:\\MOVIES\\Battel SHIP\\done-battleship-cd1.avi";
    string hh = string.Format("\"{0}\"", path);
    Process.Start(@"C:\Program Files (x86)\VideoLAN\VLC\vlc.exe ", hh + " ,--play");

The real value of hh, as passed, will be "H:\MOVIES\Battel SHIP\done-battleship-cd1.avi".

When needing double double literals, use: @"H:\MOVIES\Battel SHIP\done-battleship-cd1.avi";

Instead of: @"H:\MOVIESBattel SHIP\done-battleship-cd1.avi";

Because the first literals is for the path name and then the second literals are for the double quotation marks.

Solution 7 - C#

Using string interpolation with a working example:

var title = "Title between quotes";
var string1 = $@"<div>""{title}""</div>";  // Note the order of the $@
Console.WriteLine (string1);
Output
<div>"Title between quotes"</div>

Solution 8 - C#

You can use $.

Interpolated Strings: Used to construct strings. An interpolated string looks like a template string that contains interpolated expressions. An interpolated string returns a string that replaces the interpolated expressions that it contains with their string representations. This feature is available in C# 6 and later versions.

string commentor = $"<span class=\"fa fa-plus\" aria-hidden=\"true\"> {variable}</span>";

Solution 9 - C#

If you want to add double quotes to a string that contains dynamic values also. For the same in place of CodeId[i] and CodeName[i] you can put your dynamic values.

data = "Test ID=" + "\"" + CodeId[i] + "\"" + " Name=" + "\"" + CodeName[i] + "\"" + " Type=\"Test\";

Solution 10 - C#

You could use &quot; instead of ". It will be displayed correctly by the browser.

Solution 11 - C#

Use either

&dquo;
<div>&dquo;"+ title +@"&dquo;</div>
or escape the double quote:
"
<div>""+ title +@""</div>

Solution 12 - C#

In C# you can use:

 string1 = @"Your ""Text"" Here";
 string2 = "Your \"Text\" Here";

Solution 13 - C#

string doubleQuotedPath = string.Format(@"""{0}""",path);

Solution 14 - C#

An indirect, but simple to understand alternative to add quotes at start and end of string -

char quote = '"';

string modifiedString = quote + "Original String" + quote;

Solution 15 - C#

Put a backslash (\) before the double quotes. That should work.

Solution 16 - C#

In C#, if we use "" it means that it will indicate the following symbol is not a C# built-in symbol that will used by the developer.

So in a string we need double quotes, meaning we can put "" symbol before double quotes:

string s = "\"Hi\""

Solution 17 - C#

"<i class=\"fa fa-check-circle\"></i>" is used with ternary operator with Eval() data binding:

Text = '<%# Eval("bqtStatus").ToString()=="Verified" ? 
       Convert.ToString("<i class=\"fa fa-check-circle\"></i>") : 
       Convert.ToString("<i class=\"fa fa-info-circle\"></i>"

Solution 18 - C#

If you want to add double quotes in HTML

echo "<p>Congratulations, &#8220;" . $variable . "&#8221;!</p>";
Output
Congratulations, "Mr John"!

Solution 19 - C#

Start each row with "-" to create a bullet list.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionANPView Question on Stackoverflow
Solution 1 - C#OdedView Answer on Stackoverflow
Solution 2 - C#Fredrik MörkView Answer on Stackoverflow
Solution 3 - C#codeaView Answer on Stackoverflow
Solution 4 - C#user406570View Answer on Stackoverflow
Solution 5 - C#Yannick TurbangView Answer on Stackoverflow
Solution 6 - C#DR.View Answer on Stackoverflow
Solution 7 - C#WorkSmarterView Answer on Stackoverflow
Solution 8 - C#x19View Answer on Stackoverflow
Solution 9 - C#Ramya Prakash RoutView Answer on Stackoverflow
Solution 10 - C#JensView Answer on Stackoverflow
Solution 11 - C#grahamView Answer on Stackoverflow
Solution 12 - C#Shivam MishraView Answer on Stackoverflow
Solution 13 - C#Sunil KumarView Answer on Stackoverflow
Solution 14 - C#Abhishek PoojaryView Answer on Stackoverflow
Solution 15 - C#BelindaView Answer on Stackoverflow
Solution 16 - C#Malathi MalsView Answer on Stackoverflow
Solution 17 - C#stefanView Answer on Stackoverflow
Solution 18 - C#Yogesh SarvaiyaView Answer on Stackoverflow
Solution 19 - C#Keshav GeraView Answer on Stackoverflow