我有一个 XML 文件,我希望允许最终用户设置字符串的格式。
例如:
<Viewdata>
<Format>{0} - {1}</Format>
<Parm>Name(property of obj being formatted)</Parm>
<Parm>Phone</Parm>
</Viewdata>
所以在运行时我会以某种方式将其转换为 String.Format("{0} - {1}", usr.Name, usr.Phone);
这可能吗?
最佳答案
当然。格式字符串就是字符串。
string fmt = "{0} - {1}"; // get this from your XML somehow
string name = "Chris";
string phone = "1234567";
string name_with_phone = String.Format(fmt, name, phone);
小心使用它,因为您的最终用户可能会破坏程序。不要忘记 FormatException
。
关于c# - 在运行时设置 String.Format,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18180983/