Difference between revisions of "Formatting Strings in C Sharp"
m (Text replacement - "<table border="0" cellspacing="0" width="100%">" to "<table border="0" cellspacing="0">") |
m (Text replacement - "<hr> <table border="0" cellspacing="0"> <tr>" to "<!-- Ezoic - BottomOfPage - bottom_of_page --> <div id="ezoic-pub-ad-placeholder-114"></div> <!-- End Ezoic - BottomOfPage - bottom_of_page --> <hr> <table border="0" cellspacing="0"> <tr>") |
||
Line 100: | Line 100: | ||
+ | <!-- Ezoic - BottomOfPage - bottom_of_page --> | ||
+ | <div id="ezoic-pub-ad-placeholder-114"></div> | ||
+ | <!-- End Ezoic - BottomOfPage - bottom_of_page --> | ||
<hr> | <hr> | ||
<table border="0" cellspacing="0"> | <table border="0" cellspacing="0"> |
Revision as of 19:37, 10 May 2016
Previous | Table of Contents | Next |
Working with Strings in C# | Working with Dates and Times in C# |
Purchase and download the full PDF and ePub versions of this Visual C# eBook for only $9.99 |
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 the corresponding 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:
string newString; newString = String.Format("There are {0} cats in my {1} and no {2}", 2, "house", "dogs"); System.Console.WriteLine (newString);
When run, the above code will result in the following output:
There are 2 cats in my house and no dogs
Using Format Controls
So far we have only substituted arguments for place holders, but we have not made any changes to the format of the arguments before they are displayed. This essentially instructs the Format() method to use the default formatting for each argument type when displaying the string. Perhaps the most powerful aspect of the Format() method is the ability to use format controls within the place holders to control the format of the output.
Format controls appear inside the braces ({}) of the place holders. The format of a place holder with a format control is as follows:
{n:controlx}
Where n is the place holder number and control is the special format control sequence to be applied to the argument. The x is an optional number which further formats the output for certain controls.
As Simple Format Control Example
The following example uses the X format control to display a number in Hexadecimal format:
newString = String.Format("The number {0} in Hexadecimal is {0:X}", 432); System.Console.WriteLine (newString);
The above example displays argument 0 (the number 432) in two formats. The first is the default decimal format and the second uses the X format control to display the argument as a hexadecimal number.
The String.Format() method provides a wide range of format controls which are outlined in the following section.
The C# String.Format() Format Controls
The following table lists format controls supported by the C# String.Format() method together with examples of each control:
Control | Type | Description | Example |
---|---|---|---|
C | Currency | Displays number prefixed with the currency simple appropriate to the current locale | {0:C} of 432.00 outputs $432.00 |
D | Decimal | Displays number in decimal form with optional padding | {0:D4} of 432 outputs 00432 |
E | Exponential | Displays number in scientific form with optional value for fractional part | {0:E5} of 432.32 outputs 4.32320E+002 |
E | Fixed | Displays the number including the specified number of decimal digits | {0:F3} of 432.324343 outputs 432.324 |
N | Number | Converts a number to a human friendly format by inserting commas and rounding to the nearest 100th | {0:N} of 123432.324343 outputs 123,432.32 |
X | Hexadecimal | Converts a number to hexadecimal | {0:X} of 432 outputs 1B0 |
0:0... | Zero Padding | Adds zeros to pad argument | {0:0000.00} of 43.1 outputs 0043.10 |
0:0#... | Space Padding | Adds spaces to pad argument | {0:####.##} of 43.1 outputs 43.1 |
% | Percentage | Multiplies the argument by 100 and appends a percentage sign | {0:00.00%} of .432 outputs 43.20% |
Purchase and download the full PDF and ePub versions of this Visual C# eBook for only $9.99 |
Previous | Table of Contents | Next |
Working with Strings in C# | Working with Dates and Times in C# |