0

Say I've got a String.Format("{0} {1} at {2} for {3}-{4} ... and finally {7}", var1, var2, ...) now I have to add an extra variable at the beginning of the String.Format and for maintenance and readability reasons I want to avoid String.Format("{8} {0} {1} at {2} ...", ...).

Basically I want to avoid that I end up with String.Formats that look like String.Format("{5} {3} {0} {7} {1} {4} {2}", var1, var2, .... Something you could end up with after 6 change requests, something where you have to look very carefully where every variable is going to be substituted.

Is there something out there that can reorder the replacements for me automatically?

Laoujin
  • 9,962
  • 7
  • 42
  • 69
  • 1
    Why "avoid" the main feature of `string.Format()` ? – Alex Oct 03 '12 at 11:46
  • I will use `String.Format`, I want to minimize the time spent on reordering. Changing `"{4} {0} {1} {2} {3}"` to `"{0} {1} {2} {3} {4}"` is monkey work. Perhaps ReSharper can do this, perhaps there is some add-in, some power tool, a shortcut or macro? – Laoujin Oct 03 '12 at 12:03
  • Is there any specific reason you need to use String.Format? If you check my answer with the Aggregate you can just modify the array to get the new argument in the correct place.. – Jonas W Oct 03 '12 at 12:12
  • The above code is just an example. It's the String.Format I had to edit and I accidently had to add an extra parameter at the beginning. I'd like a general solution, one that I can use for every String.Format I need to add a parameter to. – Laoujin Oct 03 '12 at 12:16
  • It seems to me anything to do this automatically would be *at least* as much work as just changing it when you put it in. How do you picture this "automatic" mechanism working properly, without input from you as to what variable goes where anyway? – Wonko the Sane Oct 03 '12 at 12:16
  • Automating something always costs time setting up. This can be done in a pretty mechanical way: You add the new parameter where you want it. The order of the {X} is now messed up. You run the macro and it puts everything "in order" for you... – Laoujin Oct 03 '12 at 12:21
  • Yes, but it is still up to you to determine that the parameter is in the right place. Renumbering the format string is the trivial part. I'm not saying that a macro doesn't exist - I'm just saying I personally wouldn't find it that useful or even time-saving. – Wonko the Sane Oct 03 '12 at 12:41

5 Answers5

1

This construct would allow you to chain any number of variables (it's rather generic, for your particular example it will need some tweaking):

string var1 = "a", var2 = "b";
string result = string.Empty;

foreach(var a in new object[] {var1, var2 /*, var3, varN */})
{
    result = string.Format("{1} {0}", a, result);
}

I'd just use string.Format in the way you want to avoid to achieve a result with minimal effort, anyway.

Alex
  • 23,004
  • 4
  • 39
  • 73
0

Use following...

var Result=variable+String.Format("{0} {1} at {2} for {3}-{4} ... and finally {7}",var1, var2, ...)  
Amol Kolekar
  • 2,307
  • 5
  • 30
  • 45
  • didn't like it?why?i thought this was the right answer and would help Laoujin and besides all the above answers are somehow same as my answer,so why they are not downvoted? – Amol Kolekar Oct 03 '12 at 11:54
  • I didn't downvote. None of the examples are what I'm looking for. I know what String.Format is, I want something to automate a maintenance burden for me. – Laoujin Oct 03 '12 at 12:01
  • 1
    Possible reason for the downvote: Your solution is to create more Strings in memory? (using the + operator will result in one extra string being stored in memory) This sort of defeats the purpose of String.Format. – Laoujin Oct 03 '12 at 12:06
  • @AmolKolekar - The reason for the downvote was that your solution is not clean, doesn't even solve the author's non-problem, and to be frank is an ugly solution. As for the other non-answers not being downvoted, they are also ugly. – Security Hound Oct 03 '12 at 12:47
  • 1
    @Ramhound,i don't know what you are thinking but it was suppose to be simple answer to the simple question,i don't think there is any ugly coding in that,it is just a simple one line solution which came into my mind,though i gave this answer before the question updated,but most of the people thought the same way i thought.. – Amol Kolekar Oct 03 '12 at 15:25
0

Try this:

var firstString = string.Format("{0} {1} at {2} for {3}-{4} ... and finally {7}",var1, var2, ...);
var result = yourNewString + firstString;
bitsmuggler
  • 1,689
  • 2
  • 17
  • 30
0

You can do this in this way:

var var1 = String.Empty;
String.Format("{0} {1} {2} at {3} ...", var1, var2, ...);

Then if u need to add something in the beginning, u just have to set var1 value.

Nickon
  • 9,652
  • 12
  • 64
  • 119
0

Since there doesn't seem to be anything to keep string.Formats tidy (ie easier for the maintainers):

An alternative that avoids this problem altogether is to use named string formatting. Example: "{parent} yelled at {child}".FormatWith(new { parent = "I", child = "Mark" })

Example implementations (written by Phil Haack)

Also, with Roslyn we may at some point get String Interpolation which would solve this issue without needing a "custom written construct".

Community
  • 1
  • 1
Laoujin
  • 9,962
  • 7
  • 42
  • 69