12

我需要打印

a
"b"
c

使用 vebatim 字符串,我在这里提出了另一个关于多行代码模板的问题

我尝试使用逐字字符串如下:

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}\"也不行。

怎么了?

4

7 回答 7

21

试试这个(“”而不是“来逃避)

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

来自 C# 规范:http: //msdn.microsoft.com/en-us/library/Aa691090

quote-escape-sequence: ""

于 2011-05-13T18:49:41.453 回答
8

在逐字字符串文字中,您使用""双引号字符。

string line = @"
{0}
""{1}""
{2}";
于 2011-05-13T18:48:33.233 回答
4

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

    string templateString = @"
    {0}
    ""{1}""
    {2}
    ";
于 2011-05-13T18:48:31.520 回答
1

在逐字字符串中,在结果中使用""for a "

于 2011-05-13T18:48:53.990 回答
1

In an @" string, embedded double quotes are escaped as "",not \". Change your code to

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

and your problems should go away.

于 2011-05-13T18:50:10.767 回答
0
string templateString = @"        
{0}        
""{1}""
{2}
";

编辑:更新以在使用 Verbatim 时显示正确的语法。

于 2011-05-13T18:49:02.267 回答
0

Use a "double double" quote to produce a single double quote in the output. It's the same way old VB6 would process strings.

@" ""something"" is here"; 

contains a string that has quotes around the something.

于 2011-08-24T15:34:06.460 回答