Delphi字符串处理(下面贴出的是完整的可运行代码)

直接将下面的代码拷贝到IDE中是可运行的,我这里只做几点解释

1.在delphi中换行  不是\n  也不是 \r  而是  #13#10

2.取memo的行数  MContent.lines.count

3.判断字符串中某个字符的位置  Pos('m', str)  第一个参数为欲查找的字符

4.截取字串  copy(str, 7, size); 参数定义为 原字符串、开始位置(包含)、结束位置(不包含)

unit Unit1;



interface



uses

  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,

  StdCtrls,Clipbrd;



type

  TMainForm = class(TForm)

    MContent: TMemo;

    BAddText: TButton;

    SaveDialog1: TSaveDialog;

    OpenDialog1: TOpenDialog;

    BCut: TButton;

    BCopy: TButton;

    BPaste: TButton;

    BSave: TButton;

    BOpen: TButton;

    BExit: TButton;

    BClear: TButton;

    BRollback: TButton;



    procedure BAddTextClick(Sender: TObject);

    procedure BCutClick(Sender: TObject);

    procedure BSaveClick(Sender: TObject);

    procedure BOpenClick(Sender: TObject);

    procedure BCopyClick(Sender: TObject);

    procedure BPasteClick(Sender: TObject);

    procedure BExitClick(Sender: TObject);

    procedure BClearClick(Sender: TObject);

    procedure BRollbackClick(Sender: TObject);

  private

    { Private declarations }

  public

    { Public declarations }

  end;



var

  MainForm: TMainForm;



implementation



{$R *.DFM}



procedure TMainForm.BAddTextClick(Sender: TObject);

var

   before,after,current,full,str : String;

   i : Integer;

begin

    //按照行获得memo中的内容

    MContent.SelectAll;

    //如果内容为空,跳出提示

    if MContent.SelLength=0 then

    begin

      showMessage('靠!耍我啊!粘贴字段先......');

    end

    else

    begin

    //定义域添加的常量字符串

    before := 'inbuf=FML32'+ #13#10+'outbuf=FML32'+ #13#10+'export=true'+ #13#10;

    current:= 'param=';

    after  := 'type=string'+ #13#10+'access=inout'+ #13#10+'count=0'+ #13#10+'';

    full := '';

      for i := 0 to MContent.lines.count - 1 do

      begin

          str := MContent.Lines[i];

          if length(trim(str)) > 0 then

          begin

            //生成所欲要的格式

            str := UpperCase(str)+ #13#10;

            //添加到memo中

            full := full + before +  current + str + after;

            if i <> MContent.lines.count - 1 then

               full := full + #13#10;

          end

      end;

      MContent.Clear;

      MContent.text := full;

    end

end;



procedure TMainForm.BCutClick(Sender: TObject);

begin

MContent.SelectAll;

if MContent.SelLength>0 then

begin

   MContent.CutToClipboard;

   MContent.Clear;

end

end;



procedure TMainForm.BSaveClick(Sender: TObject);

begin

  if savedialog1.Execute then

    MContent.Lines.SaveToFile(savedialog1.FileName);

end;



procedure TMainForm.BOpenClick(Sender: TObject);

begin

  if Opendialog1.Execute then

    MContent.Lines.LoadFromFile(opendialog1.FileName);

end;



procedure TMainForm.BCopyClick(Sender: TObject);

begin

      MContent.selectall;

      MContent.copytoclipboard;

end;



procedure TMainForm.BPasteClick(Sender: TObject);

begin

    MContent.Clear;

    MContent.PasteFromClipboard;

end;



procedure TMainForm.BExitClick(Sender: TObject);

begin

  close;

end;



procedure TMainForm.BClearClick(Sender: TObject);

begin

  MContent.Clear;

end;



procedure TMainForm.BRollbackClick(Sender: TObject);

var

   full,str : String;

   i, size: Integer;

begin

    //按照行获得memo中的内容

    MContent.SelectAll;

    //如果内容为空,跳出提示

    if MContent.SelLength=0 then

    begin

      showMessage('靠!耍我啊!都没有东西还原什么!不能这么欺负人吧......');

    end

    else

    begin

      full := '';

      for i := 0 to MContent.lines.count - 1 do

      begin

          str  := MContent.Lines[i];

          size := length(str);

          if length(trim(str)) > 0 then

          begin

            //生成所欲要的格式

            if  Pos('m', str) > 0  then

            begin

              str := copy(str, 7, size);

              //添加到memo中

              full := full + str + #13#10;

            end

          end

      end;

      MContent.Clear;

      MContent.text := full;

    end

end;



end.



阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页