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 two specified object instances. [Edit]

public static string Format (string format, object arg0, object arg1)

Parameters

format
A string containing zero or more format specifications. [Edit]
arg0
A object to be formatted. Can be a null reference. [Edit]
arg1
A object to be formatted. Can be a null reference. [Edit]

Returns

A string containing a copy of format in which the format specifications have been replaced by the string equivalent of arg0 and arg1. [Edit]

Exceptions

TypeReason
ArgumentNullExceptionformat is a null reference. [Edit]
FormatException

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 (2).

[Edit]

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( null, format, new Object[] {arg0, arg1} ). For more information on the format specification see the string class overview.

[Edit]

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 temperature today oscillated between {0:####} and {1:####} degrees.", 78, 100) );
  Console.WriteLine( String.Format("The temperature today oscillated between {0:0000} and {1:0000} degrees.", 78, 100) );
  Console.WriteLine( "The temperature today oscillated between {0, -4} and {1, -4} degrees.", 78, 100 );
   }
}

The output is

Example
The temperature today oscillated between 78 and 100 degrees.
The temperature today oscillated between 0078 and 0100 degrees.
The temperature today oscillated between 78   and 100  degrees.

Requirements

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