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
方法的情况下获得“汤姆”的第一个字母?
你真的需要做这样的事情:
static void BuildStrings(List<string> sentences)
{
string name = "Tom";
foreach (var sentence in sentences)
Console.WriteLine(String.Format(sentence, name, name[0]));
}
static void Main(string[] args)
{
List<string> sentences = new List<string>();
sentences.Add("Hallo {0}\n");
sentences.Add("{1} is the first Letter of {0}\n");
BuildStrings(sentences);
Console.ReadLine();
}
这给了我:
Hallo Tom
T is the first Letter of Tom
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句