String.Format Method (String, Object)
Replaces one or more format items in a specified string with the string representation of a specified object.
Namespace: SystemNamespace: System
Assembly: mscorlib (in mscorlib.dll)
Parameters
- format
- Type: System.String
A composite format string (see Remarks).
- arg0
- Type: System.Object
The object to format.
Return ValueType: System.String
A copy of format in which the first format item has been replaced by the string representation of arg0.
This method uses the composite formatting feature of the .NET Framework to convert the value of an object to its string representation "}}".
Although the String.Format(String, Object) method has a single object in its parameter list, format can include more than one format item as long as each has the same index. In the following example, the format string includes two format items with the same index: one displays the decimal value of a number and the other displays its hexadecimal value.
public class Example { public static void Demo(System.Windows.Controls.TextBlock outputBlock) { outputBlock.FontFamily = new FontFamily("Courier New"); short[] values = { Int16.MinValue, -27, 0, 1042, Int16.MaxValue }; outputBlock.Text += String.Format("{0,10} {1,10}\n\n", "Decimal", "Hex"); foreach (short value in values) { string formatString = String.Format("{0,10:G}: {0,10:X}", value); outputBlock.Text += formatString + "\n"; } } } // The example displays the following output: // Decimal Hex // // -32768: 8000 // -27: FFE5 // 0: 0 // 1042: 412 // 32767: 7FFF
If the value of format is, "Thank you for your purchase of {0:####} copies of Microsoft®.NET (Core Reference).", and arg0 code example uses the Format(String, Object) method to embed an individual's age in the middle of a string.
using System; [assembly: CLSCompliant(true)] public class Example { public static void Demo(System.Windows.Controls.TextBlock outputBlock) {. if (birthdate.AddYears(years) <= dateValue) outputBlock.Text += String.Format("You are now {0} years old.", years) + "\n"; else outputBlock.Text += String.Format("You are now {0} years old.", years - 1) + "\n"; } } } // The example displays the following output: // You are now 0 years old. // You are now 1 years old. // You are now 7 years old. // You are now 9 years old. // You are now 13 years old.
For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.