[c#] How to add double quotes to a string that is inside a variable?

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 to add the double quotes here? So that it will display like :

"How to add double quotes"

This question is related to c# asp.net double-quotes

The answer is


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";

Use either

&dquo;
<div>&dquo;"+ title +@"&dquo;</div>

or escape the double quote:

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

Using string interpolation with 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>

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


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


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

char quote = '"';

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

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\";

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

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

"<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>"

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 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.

Hope this help.


You can also include the double quotes into single quotes.

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

If you want to add double quotes in HTML

echo "<p>Congratulations,  &#8220; ". $variable ." &#8221;!</p>";
output -> Congratulations, "Mr Jonh "!

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


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

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

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>";

In C# you can use:

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

in C# if we use "\" means that will indicate following symbol is not c# inbuild symbol that will use by developer. so in string we need double quotes means we can put "\" symbol before double quotes. string s = "\"Hi\""


Examples related to c#

How can I convert this one line of ActionScript to C#? Microsoft Advertising SDK doesn't deliverer ads How to use a global array in C#? How to correctly write async method? C# - insert values from file into two arrays Uploading into folder in FTP? Are these methods thread safe? dotnet ef not found in .NET Core 3 HTTP Error 500.30 - ANCM In-Process Start Failure Best way to "push" into C# array

Examples related to asp.net

RegisterStartupScript from code behind not working when Update Panel is used You must add a reference to assembly 'netstandard, Version=2.0.0.0 No authenticationScheme was specified, and there was no DefaultChallengeScheme found with default authentification and custom authorization How to use log4net in Asp.net core 2.0 Visual Studio 2017 error: Unable to start program, An operation is not legal in the current state How to create roles in ASP.NET Core and assign them to users? How to handle Uncaught (in promise) DOMException: The play() request was interrupted by a call to pause() ASP.NET Core Web API Authentication Could not load file or assembly 'CrystalDecisions.ReportAppServer.CommLayer, Version=13.0.2000.0 WebForms UnobtrusiveValidationMode requires a ScriptResourceMapping for jquery

Examples related to double-quotes

Adding double quote delimiters into csv file Add php variable inside echo statement as href link address? Calling one Bash script from another Script passing it arguments with quotes and spaces Escape double quotes in a string How do I put double quotes in a string in vba? PHP JSON String, escape Double Quotes for JS output How to add double quotes to a string that is inside a variable? How can I make Java print quotes, like "Hello"? How to include quotes in a string