简体   繁体   English

如何从Console.Write()转义{0}

[英]How to escape {0} from Console.Write()

Consider below output 考虑以下输出

'{0}' is replaced to 'Hello'

To print above text I can write this 要在文本上方打印,我可以这样写

Console.Write("'{0}' is replaced to '{1}'", "{0}", "Hello");

But here I want to use single argument to be passed for that string 但是在这里我想使用单个参数传递给该字符串

Console.Write("'{0}' is replaced to '{0}'", "Hello");
                 ^to escape somehow this

I want to escape first '{0}'. 我想先转义“ {0}”。 I tried this 我试过了

Console.Write("'\\{0}' is replaced to '{0}'", "Hello");

Output 输出量

'\Hello' is replaced to 'Hello'

Is there any way to print '{0}' instead of replacing it with first argument of that string (ie second argument of Console.Write(...) ) 有什么方法可以打印“ {0}”,而不是用该字符串的第一个参数(即Console.Write(...)的第二个参数)替换。

For string.Format and methods that use string args ie Console.Write() and Console.WriteLine() or String Interpolation , you use double braces 对于使用字符串参数的string.Format和方法,即Console.Write()Console.WriteLine()String Interpolation ,请使用双括号

Ie

use {{ blah }} to encode literal { blah } . 使用{{ blah }}对文字{ blah }进行编码。

or 要么

use {{0}} to encode literal {0} . 使用{{0}}来编码文字{0}

use I need braces {{ for some reason to encode I need braces { for some reason 使用I need braces {{ for some reason编码I need braces { for some reason

string.Format Documentation string.Format文档

How do I include literal braces ("{" and "}") in the result string? 如何在结果字符串中包含文字括号(“ {”和“}”)? For example, how do you prevent the following method call from throwing a FormatException exception? 例如,如何防止以下方法调用引发FormatException异常?

result = String.Format("The text has {0} '{' characters and {1} '}' characters.", nOpen, nClose);

A single opening or closing brace is always interpreted as the beginning or end of a format item. 单个大括号或大括号始终被解释为格式项的开头或结尾。 To be interpreted literally, it must be escaped. 要从字面上解释,必须对其进行转义。 You escape a brace by adding another brace ("{{" and "}}" instead of "{" and "}"), as in the following method call: 通过添加另一个花括号(“ {{”和“}}”而不是“ {”和“}”)来逃避花括号,如以下方法调用所示:

result = String.Format("The text has {0} '{{' characters and {1} '}}' characters.", nOpen, nClose);

However, even escaped braces are easily misinterpreted. 但是,即使是逃脱的大括号也容易被误解。 We recommend that you include braces in the format list and use format items to insert them in the result string, as the following example shows. 我们建议您在格式列表中包括括号,并使用格式项将其插入结果字符串,如以下示例所示。

result = String.Format("The text has {0} '{1}' characters and {2} '{3}' characters.", nOpen, "{", nClose, "}");

using Console.Write("\\'{0}\\' is replaced to '{1}'", "{0}", "Hello"); 使用Console.Write("\\'{0}\\' is replaced to '{1}'", "{0}", "Hello");

Output : '{0}' is replaced to 'Hello' 输出: '{0}' is replaced to 'Hello'

using Console.Write("{{0}} is replaced to '{1}'", "{0}", "Hello"); 使用Console.Write("{{0}} is replaced to '{1}'", "{0}", "Hello");

Output : {0} is replaced to 'Hello' 输出: {0} is replaced to 'Hello'

In C#6 and above you cans use Interpolated Strings : C#6及更高版本中,您可以使用插值字符串

string hello = "Hello";
Console.WriteLine($"'{{0}}' is replaced to '{hello}'");

Or below C#6 或低于C#6

string hello = "Hello";
Console.WriteLine("'{{0}}' is replaced to '{0}'", hello);
Console.Write("'{{0}}' is replaced to '{0}'", "Hello");

This is that what you want : 这就是您想要的:
1. Console.WriteLine($"{{0}} is replaced to '{"Hello"}'"); 1. Console.WriteLine($"{{0}} is replaced to '{"Hello"}'");
2. Console.WriteLine("{{0}} is replaced to '{0}'","Hello"); 2. Console.WriteLine("{{0}} is replaced to '{0}'","Hello");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM