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

string title = string.empty;

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

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

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

"How to add double quotes"

当前回答

使用工作示例的字符串插值:

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

输出

<div>"Title between quotes"</div>

其他回答

<i class=\"fa fa-check-circle\"></i>"与Eval()数据绑定的三元运算符一起使用:

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

一种间接的,但简单易懂的替代方法,在字符串的开头和结尾添加引号-

char quote = '"';

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

现在是c# 11

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

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

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

使用工作示例的字符串插值:

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

输出

<div>"Title between quotes"</div>