Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过Delphi中的备忘录进行搜索?_Delphi - Fatal编程技术网

通过Delphi中的备忘录进行搜索?

通过Delphi中的备忘录进行搜索?,delphi,Delphi,谁能给我一些简单的代码,让我能够在备忘录中搜索一个简单的字符串,并在找到后在备忘录中突出显示它 function TForm1.FindText( const aPatternToFind: String):Boolean; var p: Integer; begin p := pos(aPatternToFind, Memo1.Text); Result := (p > 0); if Result then begin

谁能给我一些简单的代码,让我能够在备忘录中搜索一个简单的字符串,并在找到后在备忘录中突出显示它

  function TForm1.FindText( const aPatternToFind: String):Boolean;
  var
    p: Integer;
  begin
    p := pos(aPatternToFind, Memo1.Text);
    Result :=  (p > 0);
    if Result then
      begin
        Memo1.SelStart := p;
        Memo1.SelLength := Length(aPatternToFind);
        Memo1.SetFocus; // necessary so highlight is visible
      end;
  end;

如果WordWrap为true,则不会跨行搜索。

此搜索允许文档换行、区分大小写的搜索和从光标位置进行搜索

type
  TSearchOption = (soIgnoreCase, soFromStart, soWrap);
  TSearchOptions = set of TSearchOption;


function SearchText(
    Control: TCustomEdit; 
    Search: string; 
    SearchOptions: TSearchOptions): Boolean;
var
  Text: string;
  Index: Integer;
begin
  if soIgnoreCase in SearchOptions then
  begin
    Search := UpperCase(Search);
    Text := UpperCase(Control.Text);
  end
  else
    Text := Control.Text;

  Index := 0;
  if not (soFromStart in SearchOptions) then
    Index := PosEx(Search, Text, 
         Control.SelStart + Control.SelLength + 1);

  if (Index = 0) and 
      ((soFromStart in SearchOptions) or 
       (soWrap in SearchOptions)) then
    Index := PosEx(Search, Text, 1);

  Result := Index > 0;
  if Result then
  begin
    Control.SelStart := Index - 1;
    Control.SelLength := Length(Search);
  end;
end;
您可以在备忘录上设置HideSelection=False以显示所选内容,即使备忘录未聚焦

这样使用:

  SearchText(Memo1, Edit1.Text, []);

也允许搜索编辑。

使用大写可能无法获得所需的结果,例如,在法语中,大写字符不能有重音,而小写字符可以有重音(我认为这与加拿大法语不同,加拿大法语中大写字符也可以有重音)。所以,在这种情况下,使用小写字母会得到更好的结果。重音字母和非重音字母是两个不同的字母,不是吗?一个法语单词,当转换成大写时会显示为PRIVé,而小写时则为PRIVé。另一方面,大写字母É也不会转换为é,所以我不知道这会对搜索结果产生什么影响。尽管我必须承认我正在Delphi7中测试这个。如果使用unicode或Delphi中可能使用的区域设置在使用小写时产生更好的结果,请这样做。GolezTrol:感谢HideSelection提示!在
TStrings.Text
而不是
TStrings.Strings
中搜索非常昂贵。事实上是2×N。@user205376-你为什么这么说?Text是一个字符串,我认为pos()会很快。正如您所建议的,搜索TStrings.Strings将涉及访问每个字符串的下标。依我看,这会更慢,而且不会提供pos()没有的额外功能。(而且,它需要一些复杂的代码来突出显示找到的模式)为什么不呢?游戏开始有点晚了,但对于上面的内容,这取决于实现。毕竟,字符串是抽象的。Memo.Lines.Text(=TMemoStrings.Text)使用单个API调用获取备忘录的文本,而TStringList存储单独的字符串和TStringList.Text在每个请求中将它们组合成单个字符串(顺便说一句,这通常比调用API更快)。答案是使用Memo1.Text,我认为它绕过了任何tstring子体,直接调用了memo句柄上的一些GetText api。