Mono Class Library: System.String Overview | Members

System.String.Format Method

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

public static string Format (string format, object arg0)

Parameters

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

Returns

A copy of format in which the first format specification has been replaced by the formatted string equivalent of the arg0.

Exceptions

TypeReason
ArgumentNullExceptionformat is a null reference.
FormatException

The format specification in 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 (1).

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} ). 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 high temperature today was {0:###} degrees.", 88));
 Console.WriteLine("The museum had {0,-6} visitors today.", 88);
 }
}

The output is

Example

The high temperature today was 88 degrees.
The museum had 88     visitors today.

Requirements

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