如何将 " 字符添加到 C# 中的多行字符串声明中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/673744/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 12:53:06  来源:igfitidea点击:

How can I add " character to a multi line string declaration in C#?

c#stringescaping

提问by User

If I write something like this:

如果我写这样的东西:

string s = @"...."......";

it doesn't work.

它不起作用。



If I try this:

如果我试试这个:

string s = @"...\".....";

it doesn't work either.

它也不起作用。

How can I add a " character to a multi line string declaration in C#?

如何将 " 字符添加到 C# 中的多行字符串声明中?

采纳答案by mqp

Try this:

尝试这个:

string s = @"..."".....";

回答by Gordon Mackie JoanMiro

The double character usage also works with the characters {and }when you're using string.Formatand you want to include a literal instance of either rather than indicate a parameter argument, for example:

双字符用法也适用于字符{}当您使用string.Format并且您想要包含两者的文字实例而不是指示参数参数时,例如:

string jsString = string.Format(
    "var jsonUrls = {{firstUrl: '{0}', secondUrl: '{1}'}};",
    firstUrl,
    secondUrl
    );

回答by Crash893

string s = "...\".....";should work

字符串s = "...\".....";应该工作

the @ disables escapes so if you want to use \" then no @ symbol

@ 禁用转义,所以如果你想使用 \" 那么没有 @ 符号

Personally i think you should go with

我个人认为你应该去

string s = string.format("{0}\"{1},"something","something else"); 

it makes it easier in the long run

从长远来看,这使它更容易