如何才能在memo中进行查找和继续查找

memo1.Lines.IndexOf(你要查找的字符串),返回这个字符串的位置,如果列表中无匹配字符串,将返回-   1。
楼上的哥哥您发了个错误,希望您平时写程序不是这样,而是因为回答问题才写错的


var   i:longing;
begin
i:=memo1.Lines.IndexOf( 'terry ');
if   i=-1   then
    showmessage( '没有这个字符串 ')
    else
    showmessage( '该字符串位于第 '+inttostr(i)+ '行 ');
end;

这个问题虽小,但如果按您的写法第一效率低,第二因为有两个地方私用 'terry '所以容易出现bug

 

 

=======================================

可以用下面的方法实现.
unction TForm1.ReplacememoStr(const FindStr, ReplaceStr: String): Integer;
var
 i: Integer;
begin
 Result := -1;
 for i := 0 to mmo1.Lines.Count - 1 do
 begin
   if Pos(FindStr, mmo1.Lines[i]) > 0 then
   begin
     Result := i;
     mmo1.Lines[Result] := ReplaceStr;
     Break;
   end;
 end;
end;
我现在用的就是这种方法,太费时了,如果memo1内容多的话就要花较长时间!
有没有效率更高一点的。
用IndexOf是一行一行匹配,应该要比pos那样快吧

[code=Delphi(Pascal)]
function TForm1.ReplaceLineNo(const FindStr, ReplaceStr: String): Integer;
var
 i: Integer;
begin
 Result := -1;
 Result := memo1.Lines.IndexOf(FindStr) ;
 if Result > -1 then
   memo1.Lines[Result-1] := ReplaceStr;    //值的上一行是行号
end;
[code]
--------------------------------------------------------------------------
我想用em_LineFromChar读取光标所在行的行号
所以你可以这样:
procedure   TForm1.Button1Click(Sender:   TObject);
begin
    Memo1.Lines.LoadFromFile( 'E:\Program\ini\安装程序.txt ');
    //   Label1.Caption:=inttostr(Memo1.Lines.IndexOf(Memo1.Lines.Strings[1]));
    Label1.Caption:=inttostr(Memo1.CaretPos.Y);
end;

procedure   TForm1.Button1Click(Sender:   TObject);
begin
    Memo1.Lines.LoadFromFile( 'E:\Program\ini\安装程序.txt ');
    Label1.Caption:=inttostr(Memo1.Lines.IndexOf(Memo1.Lines.Strings[1]));
end;
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值