#17 – Methods that Support Composite Formatting

The .NET Framework includes a number of methods that take composite format strings, allowing substitution of format items into a format string.  Methods that take composite format strings include:

  • Console.Write / WriteLine
  • Debug.WriteLine
  • StreamWriter.Write / .WriteLine
  • String.Format
  • StringBuilder.AppendFormat
  • StringWriter.Write / .WriteLine
  • TextWriter.Write / .WriteLine

So not only can you do composite formatting with String.Format:

string sNew = string.Format("Who is {0}? ({1})", "John Galt", "Rand");

You can also do the same thing with Console.WriteLine:

Console.WriteLine("Who is {0}? ({1})", "John Galt", "Rand");
Advertisement

#16 – Use an Array of Objects for a Composite Format String

Instead of passing in a long list of format items when calling a method like String.Format that deals with composite format strings,  you can also just pass in an array of objects.  Here’s an example:

object[] args = { "Sean", 36, "Sexton", "C#" };
string sTest = string.Format("{0} {2} has been programming for {1} yrs and loves {3}.", args);

The resulting string here would be:  Sean Sexton has been programming for 36 yrs and loves C#.

#15 – Using Long Lists of Format Items in Composite Format Strings

When doing composite formatting in C# for functions that take a format string and a list of format items (e.g. String.Format), you can include any number of format items after the format string.  In other words, you could have a very long list of objects that will be substituted into the format string, like the following:

string sTest5 = string.Format("blah {0} {1} {2} {3} {4} {5} {6} {7} {8} {9} {10} ", n1, n2, n3, n4, n5, n6, n7, n8, n9, n10, n11);

You can do this because the String.Format method actually takes an array of objects as its second parameter:

public static string Format(string format, params Object[] args)

Because of the params keyword, you can pass in either an array of objects after the format string, or just a long list of parameters, which will be treated as an array of objects and used in the string substitution.

#14 – Composite Format Strings in C#

When working with C#, you’ll often want to create a string that is composed of some combination of static text and substituted variable values.  You do this by specifying a format string that contains a list of placeholders (format items) where the variable values will be substituted.  You also specify the actual values of the variables to be substituted.

Below is an example.  Here we substitute both string and integer variables.  The format items within the format string are indicated with a zero-based index surrounded by curly braces.  The index indicates which object in the list should be substituted.

    string name = "Sean";
    int age = 46;
    string sFinal = string.Format("{0} is {1} yrs old. {0} is old.", name, age);

This results in the string: Sean is 46 yrs old. Sean is old.

See also: Composite Formatting topic in MSDN.