把“”使用 C# 逐字字符串

发布于 2024-11-06 22:46:47 字数 697 浏览 7 评论 0原文

我需要

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}\" 也不起作用。

怎么了?

I need to print

a
"b"
c

with the vebatim string, I posed another question about multiple line code template here.

I tried with verbatim string as follows :

using System;

class DoFile {

    static void Main(string[] args) {
        string templateString = @"
        {0}
        \\"{1}\\"
        {2}
        ";
        Console.WriteLine(templateString, "a", "b", "c");
    }
}

But, I got this error.

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}\" doesn't work neither.

What's wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

蝶…霜飞 2024-11-13 22:46:49
string templateString = @"        
{0}        
""{1}""
{2}
";

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

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

EDIT: Updated to show correct syntax when using Verbatim.

难如初 2024-11-13 22:46:49

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

@" ""something"" is here"; 

包含一个字符串,该字符串在某些内容周围有引号。

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.

奈何桥上唱咆哮 2024-11-13 22:46:48

试试这个(“”而不是“来转义)

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

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

引用转义序列:
“”

Try this ( "" instead of " to escape )

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

From C# specification: http://msdn.microsoft.com/en-us/library/Aa691090

quote-escape-sequence:
""

情深已缘浅 2024-11-13 22:46:48

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

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

In a verbatim string literal you use "" for double quote characters.

string line = @"
{0}
""{1}""
{2}";
ㄖ落Θ余辉 2024-11-13 22:46:48

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

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

When using a multi-line string literal in C# with @", the correct escape sequence for a double-quote becomes "" instead of \".

    string templateString = @"
    {0}
    ""{1}""
    {2}
    ";
路还长,别太狂 2024-11-13 22:46:48

在逐字字符串中,使用 "" 作为结果中的 "

In a verbatim string, use "" for a " in the result.

街角迷惘 2024-11-13 22:46:48

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

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

,您的问题就会消失。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文