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.



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值