【发布时间】:2011-05-13 19:36:40
【问题描述】:
如何在 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
【问题讨论】:
-
能否包含失败的代码行?看起来您正在使用 .Format 的变体,它确实对待 { 特别。
-
这是因为使用了
Format,而不是因为使用了逐字字符串。
标签: c# .net stringtemplate