随笔 - 2146  文章 - 97 评论 - 11767 trackbacks - 253


以前没见过这个类, 它是在 TStringBuilder 的基础上实现的; 使用它可以方便地把各种数据类型写入字符串.

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    Button5: TButton;
    Button6: TButton;
    Button7: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure Button6Click(Sender: TObject);
    procedure Button7Click(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

// 其 WriteLine、Write 各有写入多种数据类型的重载
procedure TForm1.Button1Click(Sender: TObject);
var
  Writer: TStringWriter;
begin
  Writer := TStringWriter.Create;
  Writer.WriteLine('aaa');
  Writer.WriteLine('bbb');
  ShowMessage(Writer.ToString);
  Writer.Free;

  Writer := TStringWriter.Create;
  Writer.Write('aaa');
  Writer.Write('bbb');
  Writer.WriteLine;
  Writer.Write('ccc');
  ShowMessage(Writer.ToString);
  Writer.Free;
end;

//写入 Boolean、Char
procedure TForm1.Button2Click(Sender: TObject);
const
  b1: Boolean = True;
  b2: Boolean = False;
  c1: Char = 'A';
  c2: Char = 'B';
var
  Writer: TStringWriter;
begin
  Writer := TStringWriter.Create;
  Writer.WriteLine(b1);
  Writer.WriteLine(b2);
  Writer.WriteLine(c1);
  Writer.WriteLine(c2);
  ShowMessage(Writer.ToString);
  Writer.Free;
end;

//写入整数和浮点数
procedure TForm1.Button3Click(Sender: TObject);
const
  n1 = 1;
  n2 = -1;
  f = 3.14;
var
  Writer: TStringWriter;
begin
  Writer := TStringWriter.Create;
  Writer.WriteLine(n1);
  Writer.WriteLine(n2);
  Writer.WriteLine(f);
  ShowMessage(Writer.ToString);
  Writer.Free;
end;

//格式化写入
procedure TForm1.Button4Click(Sender: TObject);
var
  Writer: TStringWriter;
begin
  Writer := TStringWriter.Create;
  Writer.Write('%d + %d = %d', [1,2,3]);
  ShowMessage(Writer.ToString);
  Writer.Free;
end;

//写入字符数组
procedure TForm1.Button5Click(Sender: TObject);
var
  Writer: TStringWriter;
  cs: TCharArray;
  i: Integer;
begin
  SetLength(cs, 26);
  for i := 0 to Length(cs) - 1 do cs[i] := Chr(65+i);

  Writer := TStringWriter.Create;
  Writer.WriteLine(cs);
  Writer.WriteLine(cs, 1, 3);
  ShowMessage(Writer.ToString);
  Writer.Free;
end;

//写入对象(类名)
procedure TForm1.Button6Click(Sender: TObject);
var
  Writer: TStringWriter;
begin
  Writer := TStringWriter.Create;
  Writer.WriteLine(Self);
  Writer.WriteLine(Sender);
  ShowMessage(Writer.ToString);
  Writer.Free;
end;

//从 TStringBuilder 建立
procedure TForm1.Button7Click(Sender: TObject);
var
  Writer: TStringWriter;
  sb: TStringBuilder;
begin
  sb := TStringBuilder.Create('ABC');

  Writer := TStringWriter.Create(sb);
  Writer.WriteLine;
  Writer.Write(123);
  ShowMessage(Writer.ToString);

  sb.Free;
  Writer.Free;
end;

end.

posted on 2009-10-12 15:31  万一  阅读(...)  评论(...编辑  收藏