ANP*_*ANP 152 c# asp.net double-quotes
我有变量像:
string title = string.empty;
Run Code Online (Sandbox Code Playgroud)
我需要的是,无论传递给它的是什么字符串,我都必须用双引号显示div中的内容.所以我写了类似的东西:
...
...
<div>"+ title +@"</div>
...
...
Run Code Online (Sandbox Code Playgroud)
但是如何在这里添加双引号?所以它会显示如下:
"How to add double quotes"
Run Code Online (Sandbox Code Playgroud)
Ode*_*ded 298
你需要通过加倍它们来逃避它们(逐字字符串文字):
string str = @"""How to add doublequotes""";
Run Code Online (Sandbox Code Playgroud)
或用正常的字符串文本中,有逃脱他们\:
string str = "\"How to add doublequotes\"";
Run Code Online (Sandbox Code Playgroud)
Fre*_*örk 45
所以你基本上问如何在字符串变量中存储双引号?两个解决方案:
var string1 = @"""inside quotes""";
var string2 = "\"inside quotes\"";
Run Code Online (Sandbox Code Playgroud)
为了更清楚地说明发生了什么:
var string1 = @"before ""inside"" after";
var string2 = "before \"inside\" after";
Run Code Online (Sandbox Code Playgroud)
sch*_*lig 13
如果我理解你的问题,也许你可以试试这个:
string title = string.Format("<div>\"{0}\"</div>", "some text");
Run Code Online (Sandbox Code Playgroud)
cod*_*dea 13
如果你必须经常这样做,并且你希望这在代码中更干净,你可能希望有一个扩展方法.
这是非常明显的代码,但我认为抓住并节省时间仍然很有用.
/// <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 + "\"";
}
Run Code Online (Sandbox Code Playgroud)
然后你可以在你喜欢的每个字符串上调用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");
Run Code Online (Sandbox Code Playgroud)
传递的hh的实际值将是"H:\ MOVIES\Battel SHIP\done-battleship-cd1.avi".
当需要双倍双字面值时使用:@"H:\ MOVIES\Battel SHIP\done-battleship-cd1.avi"; 而不是:@"H:\ MOVIESBattel SHIP\done-battleship-cd1.avi"; 因为第一个文字用于路径名,然后第二个文字用于双引号
var title = "Title between quotes";
var string1 = $@"<div>""{title}""</div>"; // Note the order of the $@
Console.WriteLine (string1);
Run Code Online (Sandbox Code Playgroud)
<div>"Title between quotes"</div>
Run Code Online (Sandbox Code Playgroud)
您也可以将双引号包括在单引号中。
string str = '"' + "How to add doublequotes" + '"';
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
470608 次 |
| 最近记录: |