`

如何才能在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;
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics