Export (0) Print
Expand All

String.Format Method (String, Object[])

Replaces the format item in a specified string with the string representation of a corresponding object in a specified array.

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

static member Format : 
        format:string * 
        args:Object[] -> string 

Parameters

format
Type: System.String
A composite format string (see Remarks).
args
Type: System.Object[]
An object array that contains zero or more objects to format.

Return Value

Type: System.String
A copy of format in which the format items have been replaced by the string representation of the corresponding objects in args.

ExceptionCondition
ArgumentNullException

format or args is a null reference (Nothing in Visual Basic).

FormatException

format is invalid.

-or-

The index of a format item is less than zero, or greater than or equal to the length of the args array.

This method uses the composite formatting feature of the .NET Framework to convert the value of an object to its text representation and 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 corresponding object.

The syntax of a format item is as follows:

{index[,length][:formatString]}

Elements in square brackets are optional. The following table describes each element. For more information about the composite formatting feature, including the syntax of a format item, see Composite Formatting.

Element

Description

index

The zero-based position in the parameter list of the object to be formatted. If the object specified by index is a null reference (Nothing in Visual Basic), the format item is replaced by String.Empty. If there is no parameter in the index position, a FormatException is thrown.

,length

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.

:formatString

A standard or custom format string that is supported by the object to be formatted. Possible values for formatString are the same as the values supported by the object's ToString(format) method. If formatString is not specified and the object to be formatted implements the IFormattable interface, a null reference (Nothing in Visual Basic) is passed as the value of the format parameter that is used as the IFormattable.ToString format string.

NoteNote

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 creates a string that contains data on the high and low temperature on a particular date. The composite format string has five format items in the C# example and six in the Visual Basic example. Two of the format items define the width of their corresponding value's string representation, and the first format item also includes a standard date and time format string.

No code example is currently available or this language may not be supported.

.NET Framework

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

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

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

Community Additions

ADD
Show:
© 2014 Microsoft