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}"
- I have an array of object (string) for the input, e.g.
var arr = new[] { "asdf", "qwer" }
- I am formatting the array using the format string, e.g.
var res = string.Format(format, arr)
What I am trying to do is to revert back the formatted string back into the array of object (string). Something like (pseudo code):
var arr2 = string.Unformat(format, res)
// when: res = "asdf-qwer"
// arr2 should be equal to arr
Anyone have experience doing something like this? I'm thinking about using regular expressions (modify the original format string, and then pass it to Regex.Matches to get the array) and run it for each placeholder in the format string. Is this feasible or is there any other more efficient solution?