テスト文字列読み込みクラス:TStringReader

3505 ワード

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.