Formatting Strings in C Sharp

From Techotopia
Revision as of 15:34, 24 January 2008 by Neil (Talk | contribs) (New page: In addition to the wide selection of string manipulation functions outlined in Working with Strings in C#, the string class also provides the ''String.F...)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

In addition to the wide selection of string manipulation functions outlined in Working with Strings in C#, the string class also provides the String.Format() method.

The primary purpose of the C# String.Format() method is to provide a mechanism for inserting string, numerical or boolean values into a string.

The Syntax of the String.Format() Method

The general syntax of the String.Format() method is as follows:

String.Format(format string", arg1, arg2, .... );

The format string is the string into which the values will be placed. Within this string are place holders which indicate the location of each value within the string. Place holders take the form of braces surrounding a number indicating argument to be substituted for the place holder. Following on from the format string is a comma separated list of arguments. There must be an argument for each of the place holders.

A Simple C# String Format Example

The following code fragment demonstrates a very simple use of the String.Format() method: