This documentation is archived and is not being maintained.

String.Format Method (String, Object)

Updated: December 2009

Replaces one or more format items in a specified string with the string representation of a specified object.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

public static string Format(
	string format,
	Object arg0


Type: System.String

A composite format string (see Remarks).

Type: System.Object

An object to format.

Return Value

Type: System.String
A copy of format in which any format items are replaced by the string representation of arg0.


format is null.


The format item in format is invalid.


The index of a format item is greater or less than zero.

This method uses the composite formatting feature of the .NET Framework to convert the value of an object to its string representation and to embed that representation in a string. The .NET Framework provides extensive formatting support, which is described in greater detail in the following formatting topics.

The format parameter consists of zero or more runs of text intermixed with zero or more indexed placeholders, called format items, that correspond to an object in the parameter list of this method. The formatting process replaces each format item with the string representation of the value of the corresponding object.

The syntax of a format item is as follows:


Elements in square brackets are optional. The following table describes each element.




The zero-based position in the parameter list of the object to be formatted. If the object specified by index is null, the format item is replaced by String.Empty. Because this overload has only a single object in its parameter list, the value of index must always be 0. If there is no parameter in the index position, a FormatException is thrown.


The minimum number of characters in the string representation of the parameter. If positive, the parameter is right-aligned; if negative, it is left-aligned.


A standard or custom format string that is supported by the parameter.


For the standard and custom format strings used with date and time values, see Standard Date and Time Format Strings and Custom Date and Time Format Strings. For the standard and custom format strings used with numeric values, see Standard Numeric Format Strings and Custom Numeric Format Strings. For the standard format strings used with enumerations, see Enumeration Format Strings.

The leading and trailing brace characters, '{' and '}', are required. To specify a single literal brace character in format, specify two leading or trailing brace characters; that is, "{{" or "}}".

Although the String.Format(String, Object) method has a single object in the parameter list, format can include more than one format item as long as each has the same index. In the following example, the format string includes two format items: one displays the decimal value of a number and the other displays its hexadecimal value.

public class Example
   public static void Main()
      short[] values= { Int16.MinValue, -27, 0, 1042, Int16.MaxValue };
      Console.WriteLine("{0,10}  {1,10}\n", "Decimal", "Hex");
      foreach (short value in values)
         string formatString = String.Format("{0,10:G}: {0,10:X}", value);
// The example displays the following output: 
//       Decimal         Hex 
//        -32768:       8000 
//           -27:       FFE5 
//             0:          0 
//          1042:        412 
//         32767:       7FFF

If the string assigned to format is "Thank you for your donation of {0:####} cans of food to our charitable organization." and arg[0] is an integer with the value 10, the return value will be "Thank you for your donation of 10 cans of food to our charitable organization."

The following example uses the Format(String, Object) method to embed an individual's age in the middle of a string.

using System;

[assembly: CLSCompliant(true)]
public class Example
   public static void Main()
      DateTime birthdate = new DateTime(1993, 7, 28);
      DateTime[] dates = { new DateTime(1993, 8, 16), 
                           new DateTime(1994, 7, 28), 
                           new DateTime(2000, 10, 16), 
                           new DateTime(2003, 7, 27), 
                           new DateTime(2007, 5, 27) };

      foreach (DateTime dateValue in dates)
         TimeSpan interval = dateValue - birthdate;
         // Get the approximate number of years, without accounting for leap years. 
         int years = ((int) interval.TotalDays) / 365;
         // See if adding the number of years exceeds dateValue.
         string output;
         if (birthdate.AddYears(years) <= dateValue) {
            output = String.Format("You are now {0} years old.", years);
         else {
            output = String.Format("You are now {0} years old.", years - 1);
// The example displays the following output: 
//       You are now 0 years old. 
//       You are now 1 years old. 
//       You are now 7 years old. 
//       You are now 9 years old. 
//       You are now 13 years old.

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0




December 2009

Replaced the example.

Customer feedback.

October 2008

Expanded the Remarks section.

Customer feedback.