【问题标题】:Allow user defined Strings in c#在 C# 中允许用户定义的字符串
【发布时间】: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


【解决方案1】:

在 C#(以及一般 .NET 中)中字符串格式化的标准解决方案是使用 the String.Format method。例如,您可以这样做:

string reult = string.Format("The Name of the Item with the Id {0} is {1}. It's Description is {2}. It has the Value {3} and the Status {4}.",
       id, name, description, value, status);

【讨论】:

  • 如果您在开发期间构建字符串,这没关系。但我想给用户设计自己的字符串的可能性。参数的位置和数量可能会有所不同。
【解决方案2】:

我怀疑您想将 {ID}{Name} 等占位符替换为对象列表中的值。您可以使用带有模式的正则表达式,例如

\{(?<key>[A-Za-z]+)\}

这将找到{something} 的所有实例,并允许您提取something 以便从您的列表中获取值。

使用Regex.Match 的重载,它接受一个输入字符串和一个MatchEvaluator 委托,可以让您获得正确的值:

var myObject = new Dictionary<string,string>(){
    {"ID","1"},  
    {"Name","Bob Jones"},
    {"Description","A very nice man"},
    {"Value","1000"},
    {"Status","New"},
};
var regex = new Regex(@"\{(?<key>[A-Za-z]+)\}");
var input = "The Name of the Item with the Id {ID} is {Name}. It's Description is {Description}. It has the Value {Value} and the Status {Status}.";
string result = regex.Replace(input, m => myObject[m.Groups["key"].Value]);

Console.WriteLine(result);

现场示例:http://rextester.com/FLGTQW95860

该示例中的字典只是为了演示正则表达式的用法 - 没有理由不能是 DataRow 或自定义对象,或任何其他属性容器。

当然,此解决方案不包含任何错误处理(例如属性不存在的占位符),如果您允许用户指定字符串,我怀疑您会希望包含它。

【讨论】:

    【解决方案3】:

    你的意思是:

    var x = String.Format("The Name of the Item with the Id {0} is {1}. It's Description is {2}. It has the Value {3} and the Status {4}.",object.Id, object.Name, object.Description, object.Value, object.Status); 
    

    还是我错过了重点?

    【讨论】:

      【解决方案4】:

      您可以允许用户在字符串中使用指定的“变量”。例如:

      var userstring = "The Name of the Item with the Id {ID} is {Name}. It's Description is {Description}. It has the Value {Value} and the Status {Status}.";
      var result = userstring.Replace("{ID}", Id).Replace("{Name}", Name).Replace("{Description}", Description).Replace("{Value}", Value).Replace("{Status}", Status);
      

      【讨论】:

      • 这正是我所需要的。如果找到参数,请替换它。非常适合我。
      【解决方案5】:

      您可以定义自己的类并覆盖 ToString:

      public class Program
      {
          static void Main(string[] args)
          {
              var aString = new MyString() {Description = "desc", Id = 1, Name = "Ja", Status = "st", Value = "Val"};
      
              var myStrings = new List<MyString>() {aString};
      
      
              foreach (MyString myString in myStrings)
              {
                  //The Name of the Item with the Id 1 is Ja. It's Description is desc. It has the Value val and the Status st.
                  var outputStringVal = myString.ToString();
                  //
              }
          }
      }
      
      public class MyString 
      {
          public int Id { get; set; }
          public string Name { get; set; }
          public string Description { get; set; }
          public string Value { get; set; }
          public string Status { get; set; }
      
          public override string ToString()
          {
              return String.Format("The Name of the Item with the Id {0} is {1}. It's Description is {2}. It has the Value {3} and the Status {4}.", this.Id, Name, Description, Value, Status);
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2017-03-04
        • 1970-01-01
        • 2017-03-08
        • 2011-07-17
        • 1970-01-01
        • 2016-12-16
        • 1970-01-01
        • 2022-12-06
        • 1970-01-01
        相关资源
        最近更新 更多