String::Format Method (String, Object, Object, Object)
Updated: September 2009
Replaces the format items in a specified string with the string representation of three specified objects.
Assembly: mscorlib (in mscorlib.dll)
public: static String^ Format( String^ format, Object^ arg0, Object^ arg1, Object^ arg2 )
Parameters
- format
- Type: System::String
A composite format string (see Remarks).
- arg0
- Type: System::Object
The first object to format.
- arg1
- Type: System::Object
The second object to format.
- arg2
- Type: System::Object
The third object to format.
Return Value
Type: System::StringA copy of format in which the format items have been replaced by the string representations of arg0, arg1, and arg2.
| Exception | Condition |
|---|---|
| ArgumentNullException | format is nullptr. |
| FormatException | format is invalid. -or- The index of a format item is less than zero, or greater than two. |
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.
For more information about the composite formatting feature supported by methods such as Format, AppendFormat, and some overloads of WriteLine, see Composite Formatting.
For more information about numeric format specifiers, see Standard Numeric Format Strings and Custom Numeric Format Strings.
For more information about date and time format specifiers, see Standard Date and Time Format Strings and Custom Date and Time Format Strings.
For more information about enumeration format specifiers, see Enumeration Format Strings.
For more information about formatting, see Formatting Types and Formatting Overview.
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.
Element | Description |
|---|---|
index | The zero-based position in the parameter list of the object to be formatted. If the object specified by index is nullptr, 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 parameter. |
Note: |
|---|
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(String, Object, Object, Object) method to create a string that illustrates the result of a Boolean And operation with two integer values. Note that the format string includes six format items, but the method has only three items in its parameter list, because each item is formatted in two different ways.
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.
Note: