2011-05-13 162 views
10

我需要打印把”在逐字字符串与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}\"也不工作。

怎么了?

+0

可能重复[我可以在逐字串字面值中使用双引号吗?](http://stackoverflow.com/questions/1928909/can-i-escape-a-double-quote-in-a-verbatim -string-literal) – Ash 2017-03-22 12:28:06

回答

3

在C#中使用带@"的多行字符串文字时,双引号的正确转义序列变为""而不是\"

string templateString = @" 
    {0} 
    ""{1}"" 
    {2} 
    "; 
7

在逐字字符串文字中,对于双引号字符使用""

string line = @" 
{0} 
""{1}"" 
{2}"; 
0

在逐字字符串,请使用""在结果"

0
string templateString = @"   
{0}   
""{1}"" 
{2} 
"; 

编辑:更新到使用逐字逐句显示正确的语法。

+0

不正确。 OP已经注意到这是错误的... – mellamokb 2011-05-13 18:49:53

+0

@mellamokb - 谢谢 - 我将首先检查我的语法 – Leons 2011-05-13 19:00:19

0

在一@"字符串,嵌入式双引号转义为"",而不是\"。将您的代码更改为

string templateString = @" 
    {0} 
    ""{1}"" 
    {2} 
    "; 

并且您的问题应该消失。

+0

然后丢失反斜杠。编辑以反映变化。 – 2011-05-13 18:51:15

0

使用“double double”引号在输出中产生一个双引号。这与旧式VB6处理字符串的方式相同。

@" ""something"" is here"; 

包含一个带有引号的字符串。