Replaces the format item in a specified string with the string representation of a corresponding object in a specified array.
- format
- A composite format string.
- args
- An object array that contains zero or more objects to format.
A copy of format in which the format items have been replaced by the string representation of the corresponding objects in args.
Type Reason ArgumentNullException format or args 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 length of the args array.
For examples and comprehensive usage information about this and other overloads of the Format method, see the erload:System.String.Format overload summary.
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 winning numbers were {0:000} {1:000} {2:000} {3:000} {4:000} today.", 5, 10, 11, 37, 42) ); Console.WriteLine( "The winning numbers were {0, -6}{1, -6}{2, -6}{3, -6}{4, -6} today.", 5, 10, 11, 37, 42 ); } }
The output is
Example
The winning numbers were 005 010 011 037 042 today. The winning numbers were 5 10 11 37 42 today.