Export (0) Print
Expand All
16 out of 175 rated this helpful - Rate this topic

String.Format Method

Updated: September 2011

Replaces each format item in a specified string with the text equivalent of a corresponding object's value.

This member is overloaded. For complete information about this member, including syntax, usage, and examples, click a name in the overload list.

  NameDescription
Public method Static member Supported by the .NET Compact Framework Supported by the XNA Framework Format(String, Object) Replaces one or more format items in a specified string with the string representation of a specified object.
Public method Static member Supported by the .NET Compact Framework Supported by the XNA Framework Format(String, Object()) Replaces the format item in a specified string with the string representation of a corresponding object in a specified array.
Public method Static member Supported by the .NET Compact Framework Supported by the XNA Framework Format(IFormatProvider, String, Object()) 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.
Public method Static member Supported by the .NET Compact Framework Supported by the XNA Framework Format(String, Object, Object) Replaces the format items in a specified string with the string representations of two specified objects.
Public method Static member Supported by the .NET Compact Framework Supported by the XNA Framework Format(String, Object, Object, Object) Replaces the format items in a specified string with the string representation of three specified objects.
Top

Each overload of the Format method uses the composite formatting feature to include zero-based indexed placeholders, called format items, in a composite format string. At run time, each format item is replaced with the string representation of the corresponding argument in a parameter list. For example, the following call to the Format method includes a format string with three format items, {0}, {1}, and {2}, and an argument list with three items.

Dim dat As Date = #1/17/2012 9:30AM# 
Dim city As String = "Chicago" 
Dim temp As Integer = -16
Dim output As String = String.Format("At {0} in {1}, the temperature was {2} degrees.", _
                                     dat, city, temp)
Console.WriteLine(output)
' The example displays the following output: 
'    At 1/17/2012 9:30:00 AM in Chicago, the temperature was -16 degrees.   

A format item has the following syntax:

{index[,alignment][ : formatString] }

Brackets denote optional elements. The opening and closing brackets are required. A format item has the following elements:

index

The zero-based index of the argument whose string representation is to be included at this position in the string.

alignment

A signed integer that indicates the total length of the field into which the argument is inserted and whether it is right-aligned (a positive integer) or left-aligned (a negative integer). If alignment is omitted, the string representation of the corresponding argument is inserted in a field with no leading or trailing spaces.

formatString

A format string that specifies the format of the corresponding argument's result string. If formatString is omitted, the corresponding argument's parameterless ToString method is called to produce its string representation. If formatString is present, the argument referenced by the format item must implement the IFormattable interface. Types that support format strings include the following:

Date

History

Reason

September 2011

Added the Remarks section.

Customer feedback.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.