c# - 在运行时设置 String.Format

标签 c#

我有一个 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/

相关文章:

javascript - 如何从自动完成而不是文本中发布值?

c# - 我应该如何将用户与域对象相关联?

c# - View 模型错误 : Object reference not set to an instance of an object

c# - 如果 T 在泛型中为空怎么办?如何省略尖括号

c# - 使用 Word 2007 作为 CMS 页面编辑器

c# - 小数点使用逗号,千位 rdlc 报告使用句点

c# - 以编程方式检查是否安装了 Windows Messaging?

c# - 通过 C# 访问 sqlite 数据库时相对路径不起作用

c# - 设置内容边框的圆角半径

c# - NHibernate future 与 CreateMultiCriteria