简体   繁体   English

Delphi - 在大型TMemo中查找文本

[英]Delphi - Find text in large TMemo

I have a TMemo which contains quite a lot of texts , 80M (about 400K lines). 我有一个包含很多文本的TMemo,80M(约400K行)。

The TMemo is set with WordWrap = FALSE, there is no need to find texts that wrapped in 2 lines. TMemo设置为WordWrap = FALSE, 无需查找包含2行的文本。

I need a fast way to find a text, from the beginning, and also find next. 我需要一种快速的方法来查找文本,从一开始,然后找到下一个。

So, I put a TEdit for putting the text to find and a TButton to find the text in the TMemo. 所以,我把一个TEdit用于放置文本以找到一个TButton来查找TMemo中的文本。

I was thinking to use Pos(), checking line by line, but that will be slow. 我正在考虑使用Pos(),逐行检查,但这将是缓慢的。 And I don't know how to determine the TMemo.Lines[index] for current cursor position. 我不知道如何确定当前光标位置的TMemo.Lines [index]。

Anyone can come up with solution? 任何人都可以提出解决方案吗?

Thanks 谢谢

UPDATE: 更新:

I found a solution from here: Search thru a memo in Delphi? 我从这里找到了一个解决方案: 通过Delphi中的备忘录进行搜索?

The SearchText() function works, fast, and very fast. SearchText()函数可以快速,快速地工作。 Took couple of seconds to search unique string at the bottom end. 花了几秒钟在底端搜索唯一的字符串。

A little addition to the previous answers : you can get the line number without selecting the pattern found, like this: 前面的答案稍加补充:您可以在不选择找到的模式的情况下获取行号,如下所示:

procedure TForm1.Button3Click(Sender: TObject);
var
  I, L: Integer;

begin
  Memo1.WordWrap:= False;
  Memo1.Lines.LoadFromFile('Windows.pas');
  I:= Pos('finalization', Memo1.Text);
  if I > 0 then begin
    L := SendMessage(Memo1.Handle, EM_LINEFROMCHAR, I - 1, 0);
    ShowMessage('Found at line ' + IntToStr(L));
// if you need to select the text found:
    Memo1.SelStart := I - 1;
    Memo1.SelLength := Length('finalization');
    Memo1.SetFocus;
  end;
end;

Note that line number is zero-based, also you should subtract 1 from Pos result to obtain zero-based offset for SendMessage and TMemo.SelStart . 请注意,行号从零开始,您也应该从Pos结果中减去1,以获得SendMessageTMemo.SelStart从零开始的偏移量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM