我有一个这样的字符串变量:
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"
你需要通过将它们加倍来转义(逐字串字面量):
string str = @"""How to add doublequotes""";
或者对于普通的字符串,你可以用\:
string str = "\"How to add doublequotes\"";
所以你本质上是问如何存储双引号在一个字符串变量?有两个解决方案:
var string1 = @"""inside quotes""";
var string2 = "\"inside quotes\"";
为了让大家更清楚发生了什么:
var string1 = @"before ""inside"" after";
var string2 = "before \"inside\" after";
如果我理解你的问题,也许你可以试试这个:
string title = string.Format("<div>\"{0}\"</div>", "some text");
使用
&dquo <div>&dquo"+ title +@"&dquo</div>
或者转义双引号:
\" <div>\""+ title +@"\"</div>
如果你必须经常这样做,并且你想让代码更干净,你可能会喜欢有一个扩展方法。
这是非常明显的代码,但我仍然认为它可以帮助您节省时间。
/// <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 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";
因为第一个字面值是路径名,第二个字面值是双引号。
使用工作示例的字符串插值:
var title = "Title between quotes";
var string1 = $@"<div>""{title}""</div>"; // Note the order of the $@
Console.WriteLine (string1);
输出
<div>"Title between quotes"</div>
在c#中,如果我们使用“”,这意味着它将表明下面的符号不是开发人员将使用的c#内置符号。
所以在字符串中,我们需要双引号,这意味着我们可以在双引号前加上""符号:
string s = "\"Hi\""
<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>"
你可以使用$。
插入字符串: 用于构造字符串。插值字符串看起来像包含插值表达式的模板字符串。插值后的字符串返回一个字符串,该字符串将其包含的插值表达式替换为它们的字符串表示形式。该特性在c# 6及更高版本中可用。
string commentor = $"<span class=\"fa fa-plus\" aria-hidden=\"true\"> {variable}</span>";
一种间接的,但简单易懂的替代方法,在字符串的开头和结尾添加引号-
char quote = '"';
string modifiedString = quote + "Original String" + quote;
如果你想在HTML中添加双引号
echo "<p>Congratulations, “" . $variable . "”!</p>";
输出
Congratulations, "Mr John"!
如果您还想向包含动态值的字符串添加双引号。同样,在CodeId[i]和CodeName[i]的地方,您可以放入动态值。
data = "Test ID=" + "\"" + CodeId[i] + "\"" + " Name=" + "\"" + CodeName[i] + "\"" + " Type=\"Test\";
在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"
""";
现在是c# 11
var string1 = """before "inside" after""";
var string2 = """ "How to add double quotes" """;