Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Named string formatting in C#

There is no built-in method for handling this.

Here's one method

string myString = "{foo} is {bar} and {yadi} is {yada}".Inject(o);

Here's another

Status.Text = "{UserName} last logged in at {LastLoginDate}".FormatWith(user);

A third improved method partially based on the two above, from Phil Haack


I have an implementation I just posted to my blog here: http://haacked.com/archive/2009/01/04/fun-with-named-formats-string-parsing-and-edge-cases.aspx

It addresses some issues that these other implementations have with brace escaping. The post has details. It does the DataBinder.Eval thing too, but is still very fast.


Interpolated strings were added into C# 6.0 and Visual Basic 14

Both were introduced through new Roslyn compiler in Visual Studio 2015.

  • C# 6.0:

    return "\{someVariable} and also \{someOtherVariable}" OR
    return $"{someVariable} and also {someOtherVariable}"

    • source: what's new in C#6.0

       

  • VB 14:

    return $"{someVariable} and also {someOtherVariable}"

    • source: what's new in VB 14

Noteworthy features (in Visual Studio 2015 IDE):

  • syntax coloring is supported - variables contained in strings are highlighted
  • refactoring is supported - when renaming, variables contained in strings get renamed, too
  • actually not only variable names, but expressions are supported - e.g. not only {index} works, but also {(index + 1).ToString().Trim()}

Enjoy! (& click "Send a Smile" in the VS)


You can also use anonymous types like this:

    public string Format(string input, object p)
    {
        foreach (PropertyDescriptor prop in TypeDescriptor.GetProperties(p))
            input = input.Replace("{" + prop.Name + "}", (prop.GetValue(p) ?? "(null)").ToString());

        return input;
    }

Of course it would require more code if you also want to parse formatting, but you can format a string using this function like:

Format("test {first} and {another}", new { first = "something", another = "something else" })