C# 两个双引号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9582781/
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
C# two double quotes
提问by sai sindhu
I want to print two double quotes in C# as the output. How to do this?
我想在 C# 中打印两个双引号作为输出。这该怎么做?
I mean the output should be: "" Hello World ""
我的意思是输出应该是: "" Hello World ""
回答by Chris
If you want to put double quotes in a string you need to escape them with a \
如果你想把双引号放在一个字符串中,你需要用 \
eg:
例如:
string foo = "here is a \"quote\" character";
If you want to literally output "" Hello World ""then you'd need:
如果你想从字面上输出,"" Hello World ""那么你需要:
string helloWorld = "\"\" Hello World \"\"";
output(helloWorld);
(where output is whatever method you are using for output)
(其中输出是您用于输出的任何方法)
回答by Jon
One way is to escape the quotes:
一种方法是转义引号:
var greeting = "\"Hello World\"";
回答by MByD
Escape them:
逃离他们:
Console.WriteLine("\"Hello world\"");
回答by Balazs Tihanyi
Console.WriteLine("\"\" Hello world \"\"");
or
或者
Console.WriteLine(@""""" Hello world """"");
回答by Diego
Use a backslash before the double quotes: \"
在双引号前使用反斜杠: \"
回答by SkonJeet
Console.WriteLine("\"\"Hello world\"\"");
The backslash ('\') character precedes any 'special' character that would otherwise be interpreted as your code instead of as part of the string to be output. It's a way of telling the compiler to treat it as a character part of a string as opposed to a character that would otherwise have some sort of purpose in the C# language.
反斜杠 ('\') 字符位于任何“特殊”字符之前,否则这些字符将被解释为您的代码而不是要输出的字符串的一部分。这是一种告诉编译器将其视为字符串的字符部分而不是在 C# 语言中具有某种用途的字符的方法。
回答by BlueCacti
Using a @-char before the 'normal' double quotes will result in printing every special char between those dubble quotes
在“普通”双引号之前使用 @-char 将导致在这些双引号之间打印每个特殊字符
string foo = @"foo "bar"";
回答by bdparrish
you can output with the @, which will automatically escape special characters.
您可以使用 输出@,它会自动转义特殊字符。
string output = "\"\" Hello World \"\"";
string output = @""""" Hello World """"";
回答by Mulesoft Developer
When you want to use special character which are exist in you language add \ before that character then special character start behaving as a string. In your case use like this
当您想使用语言中存在的特殊字符时,在该字符之前添加 \,然后特殊字符开始表现为字符串。在你的情况下使用这样
\"Hello word\"
Out put
输出
"Hello word"
回答by codea
If you have to do this often and you would like this to be cleaner in code you might like to have an extension method for this.
如果您必须经常这样做,并且您希望代码更简洁,您可能希望为此拥有一个扩展方法。
This is really obvious code, but still I think it can be useful to grab and make you save time.
这是非常明显的代码,但我仍然认为抓住并节省时间很有用。
/// <summary>
/// Put a string between double quotes.
/// </summary>
/// <param name="value">Value to be put between double quotes ex: foo</param>
/// <returns>double quoted string ex: "foo"</returns>
public static string PutIntoQuotes(this string value)
{
return "\"" + value + "\"";
}
Then you may call foo.PutIntoQuotes() or "foo".PutIntoQuotes(), on every string you like.
然后你可以在你喜欢的每个字符串上调用 foo.PutIntoQuotes() 或 "foo".PutIntoQuotes()。
Hope this help.
希望这有帮助。

