String::Format Method (IFormatProvider, String, array<Object>)
Updated: October 2008
Replaces the format item in a specified string with the string representation of a corresponding object in a specified array. A specified parameter supplies culture-specific formatting information.
Assembly: mscorlib (in mscorlib.dll)
public: static String^ Format( IFormatProvider^ provider, String^ format, ... array<Object^>^ args )
Parameters
- provider
- Type: System::IFormatProvider
An IFormatProvider implementation that supplies culture-specific formatting information.
- format
- Type: System::String
A composite format string (see Remarks).
- args
- Type: array<System::Object>
An Object array containing zero or more objects to format.
Return Value
Type: System::StringA copy of format in which the format items have been replaced by the string representation of the corresponding objects in args.
| Exception | Condition |
|---|---|
| ArgumentNullException | format or args is nullptr. |
| 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 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.
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 provider parameter supplies custom and culture-specific information used to moderate the formatting process. The provider parameter is an IFormatProvider implementation whose GetFormat method is called by the String::Format(IFormatProvider, String, array<Object>) method. The method must return an object to supply formatting information that is of the same type as the formatType parameter. The provider parameter's GetFormat method is called one or more times, depending on the specific type of objects in args, as follows:
It is always passed a Type object that represents the ICustomFormatter type.
It is passed a Type object that represents the DateTimeFormatInfo type for each format item whose corresponding data type is a date and time value.
It is passed a Type object that represents the NumberFormatInfo type for each format item whose corresponding data type is numeric.
For more information, see the Format Providers section of the Formatting Overview topic. The Example section provides an example of a custom format provider that outputs numeric values as customer account numbers with embedded hyphens.
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:
{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 there is no parameter in the index position, a FormatException is thrown. If the object specified by index is nullptr, the format item is replaced by String::Empty. |
,length | The minimum number of characters in the string representation of the object to be formatted. If positive, the object to be formatted is right-aligned; if negative, it is left-aligned. The comma is required if length is specified. |
:formatString | A standard or custom format string that is supported by the object to be formatted. If formatString is not specified and the object to be formatted implements the IFormattable interface, nullptr is passed as the value of the format parameter used as the IFormattable::ToString format string. |
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 String::Format(IFormatProvider, String, array<Object>) method to display the string representation of some date and time and numeric values using several different cultures.
The following example defines a customer number format provider that formats an integer value as a customer account number in the form x-xxxxx-xx.
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: