测试字符串读取类: TStringReader

测试字符串读取类: TStringReader


TStringReader 比 TStringWriter 的方法少得多, 分别可按: 字符、行或全部读取;

读取字符时得到的是字符编码, 全部或按行读取得到是字符串, 也可读取到字符数组.

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}

// TStringReader.Read 是按字符读取, 返回字符编码
procedure TForm1.Button1Click(Sender: TObject);
var
  Reader: TStringReader;
  i: Integer;
begin
  Reader := TStringReader.Create('ABCDEFGHIJKLMNOPQRSTUVWXYZ');

  {读取}
  i := Reader.Read;
  ShowMessageFmt('%d : %s', [i, Chr(i)]); {65 : A}

  {继续读取}
  i := Reader.Read;
  ShowMessageFmt('%d : %s', [i, Chr(i)]); {66 : B}

  {将要读取的}
  i := Reader.Peek;
  ShowMessageFmt('%d : %s', [i, Chr(i)]); {67 : C}

  Reader.Free;
end;

//试试中文
procedure TForm1.Button2Click(Sender: TObject);
var
  Reader: TStringReader;
  i: Integer;
begin
  Reader := TStringReader.Create('万一的 Delphi 博客');

  {读取}
  i := Reader.Read;
  ShowMessageFmt('%d : %s', [i, Chr(i)]); {19975 : 万}

  {继续读取}
  i := Reader.Read;
  ShowMessageFmt('%d : %s', [i, Chr(i)]); {19968 : 一}

  {将要读取的}
  i := Reader.Peek;
  ShowMessageFmt('%d : %s', [i, Chr(i)]); {30340 : 的}

  Reader.Free;
end;

//读取一行
procedure TForm1.Button3Click(Sender: TObject);
var
  Reader: TStringReader;
begin
  Reader := TStringReader.Create('123' + sLineBreak + 'abc' + sLineBreak + 'xyz');

  ShowMessage(Reader.ReadLine); {123}
  ShowMessage(Reader.ReadLine); {abc}
  ShowMessage(Reader.ReadLine); {xyz}

  Reader.Free;
end;

//一次读取全部
procedure TForm1.Button4Click(Sender: TObject);
var
  Reader: TStringReader;
begin
  Reader := TStringReader.Create('123' + sLineBreak + 'abc' + sLineBreak + 'xyz');
  ShowMessage(Reader.ReadToEnd); {123 / abc / xyz}
  Reader.Free;
end;

//按字符读取全部
procedure TForm1.Button5Click(Sender: TObject);
var
  Reader: TStringReader;
  str: string;
begin
  Reader := TStringReader.Create('123' + sLineBreak + 'abc' + sLineBreak + 'xyz');

  while Reader.Peek > 0 do str := str + Chr(Reader.Read);
  ShowMessage(str); {123 / abc / xyz}

  Reader.Free;
end;

//按行读取全部
procedure TForm1.Button6Click(Sender: TObject);
var
  Reader: TStringReader;
  str: string;
begin
  Reader := TStringReader.Create('123' + sLineBreak + 'abc' + sLineBreak + 'xyz');

  while Reader.Peek > 0 do str := str + Reader.ReadLine;
  ShowMessage(str); {123abcxyz}

  Reader.Free;
end;

//读取到字符数组
procedure TForm1.Button7Click(Sender: TObject);
var
  Reader: TStringReader;
  buf: TCharArray;
begin
  Reader := TStringReader.Create('ABCDEFGHIJKLMNOPQRSTUVWXYZ');

  SetLength(buf, 3);
  Reader.ReadBlock(buf, 0, Length(buf));
  ShowMessageFmt('%s%s%s', [buf[0], buf[1], buf[2]]); {ABC}

  Reader.Free;
end;

end.
posted on 2009-10-12 17:04  万一  阅读(4960)  评论(0)  编辑  收藏
上一篇:把一个"结构体"当做属性后碰到的问题


下一篇:如何把窗体关闭到系统托盘 - 回复 "三足乌" 的问题