我有像这样的变量:
string title = string.empty;
我需要的是,无论传递给它的是什么字符串,我都必须在div中使用双引号显示内容。所以我写了一些类似这样的东西:
...
...
<div>"+ title +@"</div>
...
...
但是我如何在这里添加双引号呢?因此,它将显示为:
"How to add double quotes"
发布于 2010-10-11 20:01:22
您需要通过将它们加倍来对它们进行转义(逐字字符串文字):
string str = @"""How to add doublequotes""";
或者使用普通字符串文字,您可以使用\
对它们进行转义
string str = "\"How to add doublequotes\"";
发布于 2010-10-11 20:01:28
因此,您实际上是在问如何在字符串变量中存储双等值?有两种解决方案:
var string1 = @"""inside quotes""";
var string2 = "\"inside quotes\"";
也许为了让事情变得更清楚一些:
var string1 = @"before ""inside"" after";
var string2 = "before \"inside\" after";
发布于 2013-05-29 15:54:32
如果你必须经常这样做,并且你想让代码更简洁,你可能会想要有一个扩展方法。
这是非常明显的代码,但我仍然认为它可以很有用的抓住它,让你节省时间。
/// <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()。
https://stackoverflow.com/questions/3905946
复制相似问题