【发布时间】:2013-03-13 08:42:08
【问题描述】:
我问的是一个非常常见的功能,我找不到任何信息。我想允许我的程序的用户使用程序中的变量创建自定义字符串。
例子:
我有一个清单:
ID Name Description Value Status
0 name 0 beschreibung 0 Wert 0 Status 0
1 name 1 beschreibung 1 Wert 1 Status 1
2 name 2 beschreibung 2 Wert 2 Status 2
3 name 3 beschreibung 3 Wert 3 Status 3
4 name 4 beschreibung 4 Wert 4 Status 4
5 name 5 beschreibung 5 Wert 5 Status 5
6 name 6 beschreibung 6 Wert 6 Status 6
7 name 7 beschreibung 7 Wert 7 Status 7
8 name 8 beschreibung 8 Wert 8 Status 8
9 name 9 beschreibung 9 Wert 9 Status 9
现在用户应该能够定义自定义字符串,例如:
ID 为 {ID} 的项目名称为 {Name}。它的描述是{描述}。它具有值 {Value} 和状态 {Status}。
我可以为这些字符串编写一个自定义解析例程,但我希望为该任务找到一个标准解决方案。有什么建议吗?
【问题讨论】:
-
您是否考虑过使用 String.Replace、String.Format 或 Regex?键(名称、描述、值和状态)是否固定?
-
除了这不很常见之外,恐怕你必须编写自己的解析例程。您必须制定规则,检查类型转换等。使用预定义的 GUI 请求数据不是更容易吗?
-
String.Replace 可以解决问题。 @JohnWillemse:好的,我认为这是一个常见功能的说法可能是一种误解。我在自动化领域工作,你不断地用我常用的参数构建信息字符串。 :)
标签: c# string parsing variables parameterized