System.String.Format Method

Replaces the format items in a specified string with the string representation of three specified objects.

Syntax

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

Parameters

format
A composite format string.
arg0
The first object to format.
arg1
The second object to format.
arg2
The third object to format.

Returns

A copy of format in which the format items have been replaced by the string representations of arg0, arg1, and arg2.

Exceptions

TypeReason
ArgumentNullException format 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 number of provided objects to be formatted (3).

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 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, 4.0.0.0