我有一个这样的字符串变量:

string title = string.empty;

我必须在双引号内的div中显示传递给它的任何内容。我写过这样的东西:

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

我怎么在这里加双引号?这样它就会像这样显示:

"How to add double quotes"

当前回答

你需要通过将它们加倍来转义(逐字串字面量):

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

或者对于普通的字符串,你可以用\:

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

其他回答

如果我理解你的问题,也许你可以试试这个:

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

另注:

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

hh的实际值,正如传递的那样,将是"H:\MOVIES\ batttel SHIP\done-battleship-cd1.avi"。

当需要双双字面值时,使用:@"H:\MOVIES\ batttel SHIP\done-battleship-cd1.avi";

而不是:@"H:\ moviesbatttel SHIP\done-battleship-cd1.avi";

因为第一个字面值是路径名,第二个字面值是双引号。

你可以使用$。

插入字符串: 用于构造字符串。插值字符串看起来像包含插值表达式的模板字符串。插值后的字符串返回一个字符串,该字符串将其包含的插值表达式替换为它们的字符串表示形式。该特性在c# 6及更高版本中可用。

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

现在是c# 11

var string1 = """before "inside" after""";

var string2 = """ "How to add double quotes" """;

你可以用&quot;而不是“。”浏览器将正确地显示它。