我有一个这样的字符串变量:
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"
当前回答
如果你必须经常这样做,并且你想让代码更干净,你可能会喜欢有一个扩展方法。
这是非常明显的代码,但我仍然认为它可以帮助您节省时间。
/// <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()。
其他回答
如果我理解你的问题,也许你可以试试这个:
string title = string.Format("<div>\"{0}\"</div>", "some text");
另注:
string path = @"H:\\MOVIES\\Battel SHIP\\done-battleship-cd1.avi";
string hh = string.Format("\"{0}\"", path);
Process.Start(@"C:\Program Files (x86)\VideoLAN\VLC\vlc.exe ", hh + " ,--play");
hh的实际值,正如传递的那样,将是"H:\MOVIES\ batttel SHIP\done-battleship-cd1.avi"。
当需要双双字面值时,使用:@"H:\MOVIES\ batttel SHIP\done-battleship-cd1.avi";
而不是:@"H:\ moviesbatttel SHIP\done-battleship-cd1.avi";
因为第一个字面值是路径名,第二个字面值是双引号。
在c#中,你可以使用:
string1 = @"Your ""Text"" Here";
string2 = "Your \"Text\" Here";
如果你必须经常这样做,并且你想让代码更干净,你可能会喜欢有一个扩展方法。
这是非常明显的代码,但我仍然认为它可以帮助您节省时间。
/// <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()。
使用工作示例的字符串插值:
var title = "Title between quotes";
var string1 = $@"<div>""{title}""</div>"; // Note the order of the $@
Console.WriteLine (string1);
输出
<div>"Title between quotes"</div>