可能重复:
如何在.Net中转义格式字符串中的括号
如何在 C# 中逐字输入{
或}
输入字符串?
using System;
class DoFile {
static void Main(string[] args) {
string templateString = @"
\{{0}\}
{1}
{2}
";
Console.WriteLine(templateString, "a", "b", "c");
}
}
当我在逐字字符串中使用 '{' 或 '}' 时出现错误,我想这是因为 '{' 或 '}' 用于参数标记,例如{0}, {1}, {2}
. \{
不起作用。
Unhandled Exception: System.FormatException: Input string was not in a correct format.
at System.String.ParseFormatSpecifier (System.String str, System.Int32& ptr, System.Int32& n, System.Int32& width, System.Boolean& left_align, System.String& format) [0x00000] in <filename unknown>:0
at System.String.FormatHelper (System.Text.StringBuilder result, IFormatProvider provider, System.String format, System.Object[] args) [0x00000] in <filename unknown>:0