可能是重复问题:
如何在 .Net 中转义格式字符串中的括号
在 C# 的逐字字符串中如何输入 {
或 }
?
using System;
class DoFile {
static void Main(string[] args) {
string templateString = @"
\{{0}\}
{1}
{2}
";
Console.WriteLine(templateString, "a", "b", "c");
}
}
使用verbatim字符串时,当我使用'{'或'}'时会出现错误,我猜测是因为'{'或'}'被用于参数标记,例如{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
Format
,而不是使用原始字符串。 - jakebasile