Description |
Lots of flexibility here! The WriteLine method adds the specified values as a string to the end of the internal string, along with a line termination.
The Value may be one of the many data types - which gets converted into its string value. Or multiple values may be passed as discrete values or via an array.
Finally, formatting may be provided to Object parameters to control their formatting into the string that gets added to the end of the internal string.
The formatting string comprises 0, 1 or more sections of plain text, interspersed with formatting string sections. These formatting sections are delimited by {} brackets.
You may use { and } in the plain text by repeating each : {{ and }} respectively.
The formatting sections each have the following general syntax :
{Index[,Width][:Formatting]}
Where :
Index | Is the parameter number (0 indexed) |
Width | Defines the whole field width |
Formatting | Defines a specific format to use |
The formatting value has the following syntax :
FormattingChar[Precision]
Unlike the non-.Net Delphi Format Run Time Library function, the .Net formatting character set is split into 3 groups, applicable to the general data type. For example, the D formatting character applied to a number yields a Decimal string, but applied to a DateTime will yield a LongDate string.
The optional Precision forces the number of decimal places or significant digits as appropriate.
1. Number formatting characters
C or c | Currency such as ?1,234.56 |
D or d | Integer formatting such as 123456 |
E or e | Exponential (Scientific) such as 1.23456E+003 |
F or f | Fixed Point such as 1234.56 |
G or g | General : type dependent |
N or n | Number : -d,ddd,ddd.ddd... format |
P or p | Percent such as 69.5% |
R or r | Round-trip - guarantees reverse formatting |
X or x | Hex such as E06FC (using X) or e06fc (using x) |
2. DateTime formatting characters
d | Short Date, such as : 24/08/2004 |
D | Long date, such as 24 August 2004 |
t | Short time, such as 12:23 |
T | Long time, such as 12:23:45 |
f | Full datetime, such as 24 August 2004 12:23 |
F | Full datetime, such as 24 August 2004 12:23:45 |
g | General datetime, such as 24/08/2004 12:23 |
G | General datetime, such as 24/08/2004 12:23:45 |
M or m | Month, such as 24 August |
R or r | Culture independant, such as Tue 24 August 2004 12:23:45 GMT |
s | Sortable datetime, such as 2004-08-24T12:23:45 |
u | Universal sortable datetime |
Y or y | Year plus month, such as August 2004 |
3. Enumeration formatting characters
D | Decimal |
F | Fixed |
G | General |
X | Hex |
See the second section of code for examples of the formatting
|
|
Microsoft MSDN Links |
System.IO
System.IO.StringWriter
|
|
|
A simple example |
program Project1;
{$APPTYPE CONSOLE}
uses
System.IO;
var
Writer : System.IO.StringWriter;
begin
// Create our String Writer
Writer := System.IO.StringWriter.Create;
// Write various things to our StringWriter
Writer.WriteLine(27);
Writer.WriteLine(true);
Writer.WriteLine(DateTime.Create(2004, 9, 17));
// Now display the contents
Console.WriteLine(Writer.ToString);
Console.Readline;
end.
| Show full unit code | 27
True
17/09/2004 00:00:00
| | Using a formatting string | program Project1;
{$APPTYPE CONSOLE}
uses
System.IO;
var
Writer : System.IO.StringWriter;
decNumber : Decimal;
intNumber : Integer;
floatNum : Single;
when : DateTime;
enum : System.DayOfWeek;
begin
decNumber := 123456.789;
intNumber := 123456789;
floatNum := 0.25;
when := DateTime.Now;
enum := DayOfWeek.Tuesday;
// Create our StringWriter
Writer := System.IO.StringWriter.Create;
// Build up lots of lines in our StringWriter
// to illustrate the formatting options
Writer.WriteLine('Number formatting examples :');
Writer.WriteLine;
Writer.WriteLine('C : Currency : {0:C}',
decNumber);
Writer.WriteLine('D : Integer (D) : {0:D}',
System.Object (intNumber));
Writer.WriteLine('E : Exponential : {0:E}',
decNumber);
Writer.WriteLine('F : Fixed : {0:F}',
decNumber);
Writer.WriteLine('G : General : {0:G}',
decNumber);
Writer.WriteLine('N : Number : {0:N}',
decNumber);
Writer.WriteLine('P : Percent : {0:P}',
System.Object (floatNum));
Writer.WriteLine('R : Round-trip : {0:R}',
System.Object (floatNum));
Writer.WriteLine('X : Hexadecimal (X): {0:X}',
System.Object (intNumber));
Writer.WriteLine('x : Hexadecimal (x): {0:x}',
System.Object (intNumber));
Writer.WriteLine;
Writer.WriteLine('DateTime formatting examples :');
Writer.WriteLine;
Writer.WriteLine('d : Short Date : {0:d}', when);
Writer.WriteLine('D : Long Date : {0:D}', when);
Writer.WriteLine('t : Short Time : {0:t}', when);
Writer.WriteLine('T : Long Time : {0:T}', when);
Writer.WriteLine('f : Full date : {0:f}', when);
Writer.WriteLine('F : Long full date : {0:F}', when);
Writer.WriteLine('g : General : {0:g}', when);
Writer.WriteLine('G : Long general : {0:G}', when);
Writer.WriteLine('M : Month : {0:M}', when);
Writer.WriteLine('R : RFC1123 : {0:R}', when);
Writer.WriteLine('s : Sortable : {0:s}', when);
Writer.WriteLine('u : Universal sort : {0:u}', when);
Writer.WriteLine('Y : Year : {0:Y}', when);
Writer.WriteLine;
Writer.WriteLine('Enumeration formatting examples :');
Writer.WriteLine;
Writer.WriteLine('G : General : {0:G}', enum);
Writer.WriteLine('F : Fixed : {0:F}', enum);
Writer.WriteLine('D : Decimal : {0:D}', enum);
Writer.WriteLine('X : Hexadecimal (X): {0:X}', enum);
Writer.WriteLine('x : Hexadecimal (x): {0:x}', enum);
// Now display the contents
Console.WriteLine(Writer.ToString);
Console.Readline;
end.
| Show full unit code | Number formatting examples :
C : Currency : ?123,456.79
D : Integer (D) : 123456789
E : Exponential : 1.234568E+005
F : Fixed : 123456.79
G : General : 123456.789
N : Number : 123,456.79
P : Percent : 25.00 %
R : Round-trip : 0.25
X : Hexadecimal (X): 75BCD15
x : Hexadecimal (x): 75bcd15
DateTime formatting examples :
d : Short Date : 17/09/2004
D : Long Date : 17 September 2004
t : Short Time : 08:57
T : Long Time : 08:57:55
f : Full date : 17 September 2004 08:57
F : Long full date : 17 September 2004 08:57:55
g : General : 17/09/2004 08:57
G : Long general : 17/09/2004 08:57:55
M : Month : 17 September
R : RFC1123 : Fri, 17 Sep 2004 08:57:55 GMT
s : Sortable : 2004-09-17T08:57:55
u : Universal sort : 2004-09-17 08:57:55Z
Y : Year : September 2004
Enumeration formatting examples :
G : General : Tuesday
F : Fixed : Tuesday
D : Decimal : 2
X : Hexadecimal (X): 00000002
x : Hexadecimal (x): 00000002
|
|
|
|