Delphi 读取文件获取指定字符串


本文整理自网络,侵删。

 
unit Unit2;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
  System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, SynEdit,
  SynMemo, Vcl.StdCtrls, Vcl.ComCtrls, Vcl.ExtCtrls;

type
  TForm2 = class(TForm)
    OpenDialog1: TOpenDialog;
    SynMemo1: TSynMemo;
    SynMemo2: TSynMemo;
    Panel1: TPanel;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form2: TForm2;

implementation

{$R *.dfm}

function IsHZ(ch: WideChar): boolean;
var
  i: integer;
begin
  i := ord(ch);
  if (i < 19968) or (i > 40869) then
    result := false
  else
    result := true;
end;

procedure TForm2.Button1Click(Sender: TObject);
var
  SS: TStringStream;
  S: string;
  sList: TStringList;
  i: integer;
  str: string;
  bz: Boolean;
begin
  if OpenDialog1.Execute then
  begin
    SS := TStringStream.Create('', TEncoding.UTF8);
    sList := TStringList.Create;
    try
      SS.LoadFromFile(OpenDialog1.Filename);
      SynMemo1.Lines.LoadFromStream(SS);
      SynMemo1.Lines.Text := UTF8Decode(SynMemo1.Lines.Text);

      S := SynMemo1.Lines.Text;
      bz := False;
      for i := 0 to S.Length - 1 do
      begin
        if (IsHZ(S[i]) or (S[i] in ['0'..'9']) or (S[i] in ['a'..'z']) or (S[i] in ['A'..'Z']) or (S[i] in ['.', '\', '-', '[', ']', ':'])) and (not (S[i] in [' '])) then
        begin
          str := str + S[i];
          bz := true;
        end
        else
          bz := False;

        if (str.Length > 0) and bz = false then
        begin
          if str.Trim <> '' then
            sList.add(str);
          str := '';
        end;
      end;
      SynMemo2.Lines := sList;
    finally
      sList.free;
      SS.Free;
    end;

  end;

end;

end.

相关阅读 >>

Delphi 文件/流的加密解密单元 unit_compress.pas

Delphi高亮显示trichedit当前行

Delphi xe更改ttrayicon系统任务栏图标(无模糊)

获取网络日期

Delphi httpget 判断链接是否可以访问

Delphi分别连接ms sqlserver、oracle和access数据库的连接字符串

Delphi 字符串加单引号

Delphi 关闭xp保护 替换explorer.exe

Delphi遍历所有控件

Delphi 10.3.x 截取字符串函数substring 和copy()常用字串符处理函数用法

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...