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

string title = string.empty;

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

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

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

"How to add double quotes"

当前回答

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

其他回答

您还可以将双引号包含到单引号中。

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

在c#中,你可以使用:

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

所以你本质上是问如何存储双引号在一个字符串变量?有两个解决方案:

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

为了让大家更清楚发生了什么:

var string1 = @"before ""inside"" after";
var string2 = "before \"inside\" after";
string doubleQuotedPath = string.Format(@"""{0}""",path);

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

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