Mono Class Library: System.String Overview | Members

System.String.Format Method

Replaces the format specification in a specified string with the textual equivalent of the value of a corresponding object instance in a specified array.

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

Parameters

format
A string containing zero or more format specifications.
args
A object array containing the objects to be formatted.

Returns

A string containing a copy of format in which the format specifications have been replaced by the string equivalent of the corresponding instances of object in args.

Exceptions

TypeReason
ArgumentNullExceptionformat 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

If an object referenced in the format string is a null reference, an empty string is used in its place.

Note: This version of string.Format(string, object) is equivalent to string.Format(string, object)( null, format, args ). For more information on the format specification see the string class overview.

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