Home  |  Delphi .net Home  |  System.IO.StringWriter  |  Write Method
Write  
Method  
Adds a character or characters to the current string without line termination
StringWriter Class
System.IO NameSpace
CF1.  Procedure Write ( Value : String ; ) ;
CF2.  Procedure Write ( Value : Object ; ) ;
CF3.  Procedure Write ( Value : Decimal ; ) ;
CF4.  Procedure Write ( Value : Single ; ) ;
CF5.  Procedure Write ( Value : Double ; ) ;
CF6.  Procedure Write ( Value : Integer ; ) ;
CF7.  Procedure Write ( Value : Int64 ; ) ;
CF8.  Procedure Write ( Value : UInt64 ; ) ;
CF9.  Procedure Write ( Value : Cardinal ; ) ;
CF10.  Procedure Write ( Value : Boolean ; ) ;
CF11.  Procedure Write ( Value : Char ; ) ;
CF12.  Procedure Write ( Value : Array of Char ; ) ;
CF13.  Procedure Write ( Value:Array of CharValue : Array of Char; Index : Integer; Count : Integer ; ) ;
CF14.  Procedure Write ( Value:FormatString:StringValue : FormatString; Value : Object ; ) ;
CF15.  Procedure Write ( Value:FormatString:StringValue : FormatString; Value1 : Object; Value2 : Object ; ) ;
CF16.  Procedure Write ( Value:FormatString:StringValue : FormatString; Value1 : Object; Value2 : Object; Value3 : Object; ) ;
CF17.  Procedure Write ( Value:FormatString:StringValue : FormatString; Values : Array of Object; ) ;
CF : Methods with this mark are Compact Framework Compatible
Description
Lots of flexibility here! The Write method adds the specified values as a string to the end of the internal string, but without 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
 
Short Date, such as : 24/08/2004
Long date, such as 24 August 2004
Short time, such as 12:23
Long time, such as 12:23:45
Full datetime, such as 24 August 2004 12:23
Full datetime, such as 24 August 2004 12:23:45
General datetime, such as 24/08/2004 12:23
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
Sortable datetime, such as 2004-08-24T12:23:45
Universal sortable datetime
Y or y Year plus month, such as August 2004

 
3. Enumeration formatting characters
 
Decimal
Fixed
General
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.Write(27);
  Writer.Write(' , ');
  Writer.Write(true);
  Writer.Write(' , ');
  Writer.Write(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.Write('C : Currency       : {0:C}',
                      decNumber);
  Writer.WriteLine;
  Writer.Write('D : Integer (D)    : {0:D}',
                      System.Object (intNumber));
  Writer.WriteLine;
  Writer.Write('E : Exponential    : {0:E}',
                      decNumber);
  Writer.WriteLine;
  Writer.Write('F : Fixed          : {0:F}',
                      decNumber);
  Writer.WriteLine;
  Writer.Write('G : General        : {0:G}',
                      decNumber);
  Writer.WriteLine;
  Writer.Write('N : Number         : {0:N}',
                      decNumber);
  Writer.WriteLine;
  Writer.Write('P : Percent        : {0:P}',
                      System.Object (floatNum));
  Writer.WriteLine;
  Writer.Write('R : Round-trip     : {0:R}',
                      System.Object (floatNum));
  Writer.WriteLine;
  Writer.Write('X : Hexadecimal (X): {0:X}',
                      System.Object (intNumber));
  Writer.WriteLine;
  Writer.Write('x : Hexadecimal (x): {0:x}',
                      System.Object (intNumber));
  Writer.WriteLine;

  Writer.WriteLine;
  Writer.WriteLine('DateTime formatting examples :');
  Writer.WriteLine;

  Writer.Write('d : Short Date     : {0:d}', when);
  Writer.WriteLine;
  Writer.Write('D : Long Date      : {0:D}', when);
  Writer.WriteLine;
  Writer.Write('t : Short Time     : {0:t}', when);
  Writer.WriteLine;
  Writer.Write('T : Long Time      : {0:T}', when);
  Writer.WriteLine;
  Writer.Write('f : Full date      : {0:f}', when);
  Writer.WriteLine;
  Writer.Write('F : Long full date : {0:F}', when);
  Writer.WriteLine;
  Writer.Write('g : General        : {0:g}', when);
  Writer.WriteLine;
  Writer.Write('G : Long general   : {0:G}', when);
  Writer.WriteLine;
  Writer.Write('M : Month          : {0:M}', when);
  Writer.WriteLine;
  Writer.Write('R : RFC1123        : {0:R}', when);
  Writer.WriteLine;
  Writer.Write('s : Sortable       : {0:s}', when);
  Writer.WriteLine;
  Writer.Write('u : Universal sort : {0:u}', when);
  Writer.WriteLine;
  Writer.Write('Y : Year           : {0:Y}', when);
  Writer.WriteLine;

  Writer.WriteLine;
  Writer.WriteLine('Enumeration formatting examples :');
  Writer.WriteLine;

  Writer.Write('G : General        : {0:G}', enum);
  Writer.WriteLine;
  Writer.Write('F : Fixed          : {0:F}', enum);
  Writer.WriteLine;
  Writer.Write('D : Decimal        : {0:D}', enum);
  Writer.WriteLine;
  Writer.Write('X : Hexadecimal (X): {0:X}', enum);
  Writer.WriteLine;
  Writer.Write('x : Hexadecimal (x): {0:x}', enum);
  Writer.WriteLine;

  // 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
 
 
Delphi Programming © Neil Moffatt All rights reserved.  |  Contact the author