System.String.Format Method

Replaces one or more format items in a specified string with the string representation of a specified object.

Syntax

public static string Format (string format, object arg0)

Parameters

format
A composite format string.
arg0
The object to format.

Returns

A copy of format in which any format items are replaced by the string representation of arg0.

Exceptions

TypeReason
ArgumentNullException format is a null reference.
FormatException

The format specification in format is invalid.

-or-

The number indicating an argument to be formatted is less than zero, or greater than or equal to the number of provided objects to be formatted (1).

Remarks

Note:

For examples and comprehensive usage information about this and other overloads of the Format method, see the erload:System.String.Format overload summary.

Example

The following example demonstrates the string.Format(string, object) method.

C# Example

using System;
public class StringFormat {
 public static void Main() {
 Console.WriteLine(String.Format("The high temperature today was {0:###} degrees.", 88));
 Console.WriteLine("The museum had {0,-6} visitors today.", 88);
 }
}

The output is

Example

The high temperature today was 88 degrees.
The museum had 88     visitors today.

Requirements

Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0, 4.0.0.0