static void BuildStrings(List<string> sentences)
{
string name = "Tom";
foreach (var sentence in sentences)
Console.WriteLine(String.Format(sentence, name));
}
static void Main(string[] args)
{
List<string> sentences = new List<string>();
sentences.Add("Hallo {0}\n");
sentences.Add("{0[0]} is the first Letter of {0}\n");
BuildStrings(sentences);
Console.ReadLine();
}
//Expected:
//Hallo Tom
//T is the first Letter of Tom
但是我收到了以下错误信息:
System.FormatException: '输入字符串的格式不正确。'
如何在不更改 BuildStrings
方法的情况下获取“Tom”的第一个字母?
FormatWith()
构建类似的东西,并且得到像"{name:left:1}"
这样的结果,然后通过创建自己的处理程序重载,使用"{name:left:1}".FormatWith(new { name })
进行调用。 - Oliver