把\”在逐字字符串与C#
问题描述:
我需要打印把”在逐字字符串与C#
a
"b"
c
与vebatim串,我提出了关于多行代码模板here另一个问题
我试图与逐字字符串如下:
using System;
class DoFile {
static void Main(string[] args) {
string templateString = @"
{0}
\\"{1}\\"
{2}
";
Console.WriteLine(templateString, "a", "b", "c");
}
}
但是,我得到这个错误。
t.cs(8,11): error CS1525: Unexpected symbol `{'
t.cs(9,0): error CS1010: Newline in constant
t.cs(10,0): error CS1010: Newline in constant
\"{1}\"
也不工作。
怎么了?
答
试试这个( “” 代替“逃脱)
string templateString = @"
{0}
""{1}""
{2}
";
从C#规格:http://msdn.microsoft.com/en-us/library/Aa691090
报价转义序列: ”“
答
在C#中使用带@"
的多行字符串文字时,双引号的正确转义序列变为""
而不是\"
。
string templateString = @"
{0}
""{1}""
{2}
";
答
在逐字字符串文字中,对于双引号字符使用""
。
string line = @"
{0}
""{1}""
{2}";
答
在逐字字符串,请使用""
在结果"
。
答
在一@"
字符串,嵌入式双引号转义为""
,而不是\"
。将您的代码更改为
string templateString = @"
{0}
""{1}""
{2}
";
并且您的问题应该消失。
+0
然后丢失反斜杠。编辑以反映变化。 – 2011-05-13 18:51:15
答
使用“double double”引号在输出中产生一个双引号。这与旧式VB6处理字符串的方式相同。
@" ""something"" is here";
包含一个带有引号的字符串。
可能重复[我可以在逐字串字面值中使用双引号吗?](http://*.com/questions/1928909/can-i-escape-a-double-quote-in-a-verbatim -string-literal) – Ash 2017-03-22 12:28:06