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

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

Parameters

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

Returns

A string containing a copy of format in which the first, second, and third format specifications have been replaced by the string equivalent of arg0, arg1, and arg2. [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 (3).

[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, arg2} ). 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. The average temperature  was {2:000} degrees.", 78, 100, 91));
      Console.WriteLine("The temperature today oscillated between {0, 4} and {1, 4} degrees. The average temperature was {2, 4}  degrees.", 78, 100, 91);
   }
}

The output is

Example
The temperature today oscillated between 78 and 100 degrees. The average temperature was 091 degrees.
The temperature today oscillated between   78 and  100 degrees. The average temperature was   91 degrees.

Requirements

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