zoukankan      html  css  js  c++  java
  • 测试字符串读取类: 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.
    
  • 相关阅读:
    [CentOS] SSH 免密钥登录
    [WPF] Wait for a moment.
    [Tool] Open Live Writer 插件更新
    [Tool] 博客园的 SyntaxHighlighter 代码高亮
    [Tool] Open Live Writer插件开发
    [Oracle] Bulk Insert Data
    [C1] C1ComboBox 的非编辑状态优化
    CentOS7用阿里云Docker Yum源在线安装Docker 17.03.2
    什么时候该用readfile() , fread(), file_get_contents(), fgets()?
    Aliyun OSS Nginx proxy module(阿里云OSS Nginx 签名代理模块)
  • 原文地址:https://www.cnblogs.com/del/p/1581842.html
Copyright © 2011-2022 走看看