Programming challenge #1: Reversing String.Format()

Posted Thu, Mar 22 2007 12:58 PM by Mischa Kroon

String.Format is a nifty little tool I use a lot in development.

I'm wondering if anyone has made a function yet to reverse it and I'm wondering what the cool implementations other people can come up with.
What do I mean by reversing it ? I mean extracting the values which have been formatted.

Basic example:
String.Format ("Hello there, {0} {1}, how you doing ??", "beautifull", "girl")

Renders:
Hello there, beautifull girl, how you doing ??
The output of the reverse string format should be:

beautifull and girl.

The input of the function:
The formatting string
The output of String.Format

The output of the function:
Can be a string array, or list of type x which also contains the type of data.

The challenge:
Doing this with the most beautifull working code in a .Net language ??
Extra points are given for efficiency of the code and for extracting the type of the formatted text.

Determining the Type of the formatted text:
String.Format can take different types of content and format them as a string represenation of a Date, Currency, Floating point number etc.
Examples

Extra
Seems like I didn't think things through enough, when there is no seperator text between 2 items like: {0}{1} or if there is seperator text which is also inside the variables it isn't possible to determine where 1 starts and another ends.  

Proposed solution: if one of the items is used more then once with a seperator they can still be deduced.
If neither of the ajoining items are used again or with no spaces again, there isn't really a correct way to split these items apart.

In which case the function can either reject the input, or merge the 2 variables as 1.



Good luck to all those who take this challenge :)

Comments

# re: Programming challenge #1: Reversing String.Format()

Thursday, March 22, 2007 4:49 AM by Ruud Campsteijn

So what should the program output when the parameters are for example:

{0} : "beautiful"

{1} : "and intelligent woman"

?

If the parameters ({0} and {1}) contain the same string that is used as separator between {0} and {1} in the format-string (in your example a space) you can't correctly/exactly determine the original parameters.

# re: Programming challenge #1: Reversing String.Format()

Thursday, March 22, 2007 5:34 AM by Mischa Kroon

You can return a string array for instance:

string(0) = "beautiful"

string(1) = "and intelligent woman"

There doesn't need to be a seperator in String.Format so there shouldn't have to be in the reverse function :)

So: "hello {0}{1}{2}" is also valid.

# re: Programming challenge #1: Reversing String.Format()

Thursday, March 22, 2007 6:01 AM by Michael Neel

This is where a regex would be used - no point in reinventing a wheel.  As the first comment points out, there is a loss of data that can't be recovered.  Consider:

{0} = "beautiful girl"

{1} = "intelligent woman"

in the format "Hello there, {0} {1}, how you doing ??" you would have:

"Hello there, beautiful girl intelligent woman, how you doing ??"

Since you can't assume a separator as you point out, the following would be valid of a reverse method:

{0} = "b"

{1} = "eautiful girl intelligent woman"

...unless I've misunderstood what you are asking.

# re: Programming challenge #1: Reversing String.Format()

Thursday, March 22, 2007 11:24 PM by Ruud Campsteijn

I meant to ask *what* the method should return - not *how* (as a string array, etc.)

You cannot determine the exact parameters if you have something like {0}{1}{2}. You need information on where parameters start and end in the final output string. If you have delimiter characters you might be able to do that, if you don't have those (as in {0}{1}{2}), how would you know where parameter 0 ends and parameter 1 starts?

# re: Programming challenge #1: Reversing String.Format()

Friday, March 23, 2007 5:24 AM by Mischa Kroon

Hmm good one, indeed there should be a seperator text between the 2.

I see, I'm sorry I wasn't clearer in the posting will edit to make it clearer.

# re: Programming challenge #1: Reversing String.Format()

Saturday, June 07, 2008 9:14 AM by gdcnCoAqrJmfP

fo069.txt;3;6

Leave a Comment

(required) 
(required) 
(optional)
(required) 
Please add 6 and 5 and type the answer here: