System.String.Format Method

Replaces the format item in a specified string with the string representation of a corresponding object in a specified array.

Syntax

public static string Format (string format, params object[] args)

Parameters

format
A composite format string.
args
An object array that contains zero or more objects to format.

Returns

A copy of format in which the format items have been replaced by the string representation of the corresponding objects in args.

Exceptions

TypeReason
ArgumentNullException format or args is a null reference.
FormatException

format is invalid.

-or-

The number indicating an argument to be formatted is less than zero, or greater than or equal to the length of the args array.

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 winning numbers were {0:000} {1:000} {2:000} {3:000} {4:000} today.", 5, 10, 11, 37, 42) );
      Console.WriteLine( "The winning numbers were {0, -6}{1, -6}{2, -6}{3, -6}{4, -6} today.", 5, 10, 11, 37, 42 );
 }
}

The output is

Example

The winning numbers were 005 010 011 037 042 today.
The winning numbers were 5     10    11    37    42     today.

Requirements

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