tags:

views:

174

answers:

3

string.Format() with it's "bla {0} bla" syntax is great. But sometimes I don't want to enumerate the placeholders. Instead I just want to map the variables sequentially in the placeholders. Is there a library that can do that?

For instance, instead of

string.Format("string1={0}, string2={1}", v1, v2)

something like

string.Format("string1={*}, string2={*}", v1, v2)
+3  A: 

You could accomplish this yourself by writing your own string extension coupled with the params keyword, assuming you're using .NET 3.5 or higher.

Edit: Got bored, the code is sloppy and error prone, but put this class in your project and using its namespace if necessary:

public static class StringExtensions
{
    public static string FormatEx(this string s, params string[] parameters)
    {
        Regex r = new Regex(@"\{\*\}", RegexOptions.IgnoreCase);

        for (int i = 0; i < parameters.Length; i++)
        {
            s = r.Replace(s, parameters[i], 1);
        }

        return s;
    }
}

Usage:

Console.WriteLine("great new {*} funtion {*}".FormatEx("one", "two"));
Langdon
Of course, now I can't use the same variable in multiple places.
Ed Swangren
Could do better than run "Regex.Replace" once for every parameter - see my answer.
romkyns
And you lose the ability to use formatting of parameters, e.g.string.Format("{*:dddd MMMM}", DateTime.Now).Whats wrong with specifying the paramter number?
Michael Baldry
@Michael: surely that's a question for the asker, not the answerer :)
romkyns
I mean that you cannot do this: String.Format("header{0}var{1}{0}footer", Environment.NewLine, someVariable);
Ed Swangren
Why are you people objecting to this in the answer? This was the point of the question. If you want to object to the question, then make a comment there.
Adam Robinson
doing "header{0}var{1}{0}footer" is sometimes necessary. Some other times i have a long list of variables in the order of replacement, and adding a variable in the middle of the list means I need to re-enumerate the placeholders.. and in that case it's a pain.
Nestor
"Why are you people objecting to this in the answer?" - I am not objecting, I am just pointing out the fact that you are losing some capability here. Perhaps that is fine for you, I just thought that it should be mentioned.
Ed Swangren
This is buggy. I would expect "{*}, {*}, {*}".FormatEx("a", "{*}", "b") to output "a, {*}, b", but yours outputs "a, b, {*}" because it replaces the replaced {*} again. Romkyns's answer below works properly.
Timwi
Timwi, why would ANYONE EVER do that? The code suggestion was an afterthought on my answer and provided in case the asker didn't know how to create an extension method or use the params keyword. Plus I already stated that the code was "buggy and error prone" when I originally posted it.
Langdon
A: 

If I'm just writing out a series of variables without special formatting, I often prefer to just concatenate them e.g.:

Console.WriteLine("string1=" + var1 + " string2=" + var2);

Note the leading space in front of "string2", to separate the two results.

Tom Bushell
+1  A: 

Here's a possibly faster version using Regex.Replace. Warning: no support for escaping the {*}, or nice error messages when you go out of range or don't supply enough arguments!

public static class ExtensionMethods
{
    private static Regex regexFormatSeq = new Regex(@"{\*}", RegexOptions.Compiled);

    public static string FormatSeq(this string format, params object[] args)
    {
        int i = 0;
        return regexFormatSeq.Replace(format, match => args[i++].ToString());
    }
}
romkyns
Congrats on posting an answer that actually works, unlike the broken one that inexplicably got accepted and voted up.
Timwi