Parsing formatted string

前端 未结 6 762
渐次进展
渐次进展 2020-12-03 14:12

I am trying to create a generic formatter/parser combination.

Example scenario:

  • I have a string for string.Format(), e.g. var format = \"{0}-{1}\
6条回答
  •  一生所求
    2020-12-03 14:38

    After formatting, you can put the resulting string and the array of objects into a dictionary with the string as key:

    Dictionary unFormatLookup = new Dictionary
    ...
    var arr = new string [] {"asdf", "qwer" };
    var res = string.Format(format, arr);
    unFormatLookup.Add(res,arr);
    

    and in Unformat method, you can simply pass a string and look up that string and return the array used:

    string [] Unformat(string res)
    {
      string [] arr;
      unFormatLoopup.TryGetValue(res,out arr); //you can also check the return value of TryGetValue and throw an exception if the input string is not in.
      return arr; 
    }
    

提交回复
热议问题