我有一个这样的字符串变量:
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"
当前回答
一种间接的,但简单易懂的替代方法,在字符串的开头和结尾添加引号-
char quote = '"';
string modifiedString = quote + "Original String" + quote;
其他回答
如果你必须经常这样做,并且你想让代码更干净,你可能会喜欢有一个扩展方法。
这是非常明显的代码,但我仍然认为它可以帮助您节省时间。
/// <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 + "\"";
}
然后你可以在你喜欢的每个字符串上调用foo. adddoublequotes()或"foo". adddoublequotes()。
使用
&dquo <div>&dquo"+ title +@"&dquo</div>
或者转义双引号:
\" <div>\""+ title +@"\"</div>
现在是c# 11
var string1 = """before "inside" after""";
var string2 = """ "How to add double quotes" """;
一种间接的,但简单易懂的替代方法,在字符串的开头和结尾添加引号-
char quote = '"';
string modifiedString = quote + "Original String" + quote;
在c#中,如果我们使用“”,这意味着它将表明下面的符号不是开发人员将使用的c#内置符号。
所以在字符串中,我们需要双引号,这意味着我们可以在双引号前加上""符号:
string s = "\"Hi\""