我有一个这样的字符串变量:
string title = string.empty;
我必须在双引号内的div中显示传递给它的任何内容。我写过这样的东西:
...
...
<div>"+ title +@"</div>
...
...
我怎么在这里加双引号?这样它就会像这样显示:
"How to add double quotes"
我有一个这样的字符串变量:
string title = string.empty;
我必须在双引号内的div中显示传递给它的任何内容。我写过这样的东西:
...
...
<div>"+ title +@"</div>
...
...
我怎么在这里加双引号?这样它就会像这样显示:
"How to add double quotes"
当前回答
如果您还想向包含动态值的字符串添加双引号。同样,在CodeId[i]和CodeName[i]的地方,您可以放入动态值。
data = "Test ID=" + "\"" + CodeId[i] + "\"" + " Name=" + "\"" + CodeName[i] + "\"" + " Type=\"Test\";
其他回答
在c#中,如果我们使用“”,这意味着它将表明下面的符号不是开发人员将使用的c#内置符号。
所以在字符串中,我们需要双引号,这意味着我们可以在双引号前加上""符号:
string s = "\"Hi\""
如果您还想向包含动态值的字符串添加双引号。同样,在CodeId[i]和CodeName[i]的地方,您可以放入动态值。
data = "Test ID=" + "\"" + CodeId[i] + "\"" + " Name=" + "\"" + CodeName[i] + "\"" + " Type=\"Test\";
你需要通过将它们加倍来转义(逐字串字面量):
string str = @"""How to add doublequotes""";
或者对于普通的字符串,你可以用\:
string str = "\"How to add doublequotes\"";
使用工作示例的字符串插值:
var title = "Title between quotes";
var string1 = $@"<div>""{title}""</div>"; // Note the order of the $@
Console.WriteLine (string1);
输出
<div>"Title between quotes"</div>
在c# 11.0预览版中,你可以使用原始字符串字面值。
Raw string literals are a new format for string literals. Raw string literals can contain arbitrary text, including whitespace, new lines, embedded quotes, and other special characters without requiring escape sequences. A raw string literal starts with at least three double-quote (""") characters. It ends with the same number of double-quote characters. Typically, a raw string literal uses three double quotes on a single line to start the string, and three double quotes on a separate line to end the string.
string str = """
"How to add double quotes"
""";