共用方式為


String.Format Method (String, Object, Object)

Microsoft Silverlight will reach end of support after October 2021. Learn more.

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

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

Syntax

'Declaration
Public Shared Function Format ( _
    format As String, _
    arg0 As Object, _
    arg1 As Object _
) As String
public static string Format(
    string format,
    Object arg0,
    Object arg1
)

Parameters

  • format
    Type: System.String
    A composite format string (see Remarks).

Return Value

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

Exceptions

Exception Condition
ArgumentNullException

format is nulla null reference (Nothing in Visual Basic).

FormatException

format is invalid.

-or-

The index of a format item is not zero or one.

Remarks

This method uses the composite formatting feature of the .NET Framework to convert the value of an object to its string 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 nulla null reference (Nothing in Visual Basic), 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.

,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, nulla 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 value of format is, "Thank you for your purchase of {0:####} copies of Microsoft®.NET (Core Reference).", and arg0 is an Int16 with the value 123, then the return value will be:

"Thank you for your purchase of 123 copies of Microsoft®.NET (Core Reference)."

If the value of format is, "Brad's dog has {0,-8:G} fleas.", arg0 is an Int16 with the value 42, (and in this example, underscores represent padding spaces) then the return value will be:

"Brad's dog has 42______ fleas."

Examples

The following example uses the Format(String, Object, Object) method to display time and temperature data stored in a generic Dictionary<TKey, TValue> 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.

Imports System.Collections.Generic

Module Example
   Public Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      Dim temperatureInfo As New Dictionary(Of Date, Double)
      temperatureInfo.Add(#6/1/2010 2:00:00 PM#, 87.46)
      temperatureInfo.Add(#12/1/2010 10:00:00 AM#, 36.81)

      outputBlock.Text &= "Temperature Information:" & vbCrLf
      outputBlock.Text &= vbCrLf
      Dim output As String
      For Each item In temperatureInfo
         output = String.Format("Temperature at {0,8:t} on {0,9:d}: {1,5:N1}°F", _
                                item.Key, item.Value)
         outputBlock.Text &= output & vbCrLf
      Next
   End Sub
End Module
' 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
using System;
using System.Collections.Generic;

public class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      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);

      outputBlock.Text += "Temperature Information:\n" + "\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);
         outputBlock.Text += output + "\n";
      }
   }
}
// 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

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.