Windows apps
Collapse the table of content
Expand the table of content
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

String.Format Method (String, Object, Object)

Updated: December 2009

Replaces the format items in a specified string with the string representations of two specified objects.

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

public static string Format(
	string format,
	Object arg0,
	Object arg1


Type: System.String

A composite format string (see Remarks).

Type: System.Object

The first object to format.

Type: System.Object

The second object to format.

Return Value

Type: System.String
A copy of format in which format items have been replaced by the string equivalents of arg0 and arg1.


format is null.


format is invalid.


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

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 two objects in its parameter list, the value of index must be 0 or 1. 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 "}}".

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 method to display time and temperature data stored in a generic Dictionary`2 object. Note that the format string has three format items, whereas there are only two objects to format. This is because the first object in the list (a date and time value) is used by two format items: The first format item displays the time in the date and time value, and the second displays the date in the date and time value.

using System;
using System.Collections.Generic;

public class Example
   public static void Main()
      Dictionary<DateTime, Double> temperatureInfo = new Dictionary<DateTime, Double>(); 
      temperatureInfo.Add(new DateTime(2010, 6, 1, 14, 0, 0), 87.46);
      temperatureInfo.Add(new DateTime(2010, 12, 1, 10, 0, 0), 36.81);

      Console.WriteLine("Temperature Information:\n");
      string output;   
      foreach (var item in temperatureInfo)
         output = String.Format("Temperature at {0,8:t} on {0,9:d}: {1,5:N1}°F", 
                                item.Key, item.Value);
// The example displays the following output: 
//       Temperature Information: 
//       Temperature at  2:00 PM on  6/1/2010:  87.5°F 
//       Temperature at 10:00 AM on 12/1/2010:  36.8°F

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.

© 2018 Microsoft