c# - 用 C# 将\"逐字输入字符串

标签 c# .net stringtemplate verbatim-string

<分区>

我需要打印

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

quote-escape-sequence: ""

关于c# - 用 C# 将\"逐字输入字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5996359/

相关文章:

java - 在 SpringMVC 中将 StringTemplate 与表单一起使用

java - 如何在 StringTemplate 中默认转义 HTML?

c# - 使用 C# 将用户添加到 Active Directory 组时遇到问题

c# - 没有指定authenticationScheme,也没有找到默认认证和自定义授权的DefaultChallengeScheme

c# - 将 Timer 类从 java 转换为 c# : scheduling tasks

.net - 系统事件和从 sleep 状态恢复

c# - 提供软件设置的最佳方式?

c# - 如何使用正则表达式进行不区分大小写的字符串替换?

.net - 如何从 .NET Core/Standard 项目中排除文件/文件夹?

java - antlr:如何在操作中使用 String.format