0

嗨,我正在尝试将文本保存到带有一些字符串的文本文件中,例如添加了学生姓名,但我有点卡住了

string iName2 = iName;
string text = "The student named {0} needs more work with plurals";
System.IO.File.WriteAllText(@"C:\Users\user\documents\visual studio 2012\Projects\Artificial_Intelligence_Project_3\WindowsFormsApplication1\Info.txt", text);`
4

2 回答 2

4

我假设iName是名字。String.Format是您需要的方法:

string text = String.Format("The student named {0} needs more work with plurals", iName);

除非您需要iName2其他地方,否则您不需要它。

除了更具可读性之外,String.Format使用+. 它允许您更改替换文本片段的顺序或省略其中一些:

string text = String.Format("{0} {1}", a, b);

// changed order within text without changing argument order!
string text2 = String.Format("{1} {0}", a, b);

如果您正在进行本地化,这尤其有用:不同的语言有不同的规则来构建短语,其中片段可能需要以不同的顺序替换。

于 2013-03-21T15:01:32.237 回答
1
string text = "The student named " + iName2 + " needs more work with plurals";
于 2013-03-21T15:02:35.427 回答