我想在C#中打印两个双引号作为输出。如何做到这一点?
我是说输出应该是:
我是说输出应该是:
"" Hello World ""
Console.WriteLine("\"\" Hello world \"\"");
或者
Console.WriteLine(@""""" Hello world """"");
\
来转义它们。string foo = "here is a \"quote\" character";
如果你想字面上输出"" Hello World ""
,那么你需要:
string helloWorld = "\"\" Hello World \"\"";
output(helloWorld);
一种方法是转义引号:
var greeting = "\"Hello World\"";
@
输出,这将自动转义特殊字符。string output = "\"\" Hello World \"\"";
string output = @""""" Hello World """"";
当你想使用特殊字符时,如果该字符存在于你的语言中,在该字符前添加\即可使该特殊字符作为字符串处理。在你的情况下,请像这样使用。
\"Hello word\"
"Hello word"
/// <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 + "\"";
}
那么你可以在任何你想要的字符串上调用foo.PutIntoQuotes()或"foo".PutIntoQuotes()。
希望这有所帮助。
转义它们:
Console.WriteLine("\"Hello world\"");
在双引号前使用反斜杠:\"
Console.WriteLine("\"\"Hello world\"\"");
在“普通”双引号之前使用@字符将导致打印出这些双引号之间的每个特殊字符
string foo = @"foo "bar"";
@""
)中,您需要使用2个引号字符,即""
。你现在的写法是错误的。原帖作者想要的应该是类似这样的:@""""" Hello World """""
。 - George Duckett