Is There An Efficient Whole Word Search Function in Delphi?

后端 未结 4 1410
灰色年华
灰色年华 2020-12-06 07:18

In Delphi 2009 or later (Unicode), are there any built-in functions or small routines written somewhere that will do a reasonably efficient whole word search where you provi

4条回答
  •  隐瞒了意图╮
    2020-12-06 07:41

    If you have function like below

    function ExistWordInString(aString:PWideChar;aSearchString:string;aSearchOptions: TStringSearchOptions): Boolean;
    var
      Size : Integer;
    Begin
          Size:=StrLen(aString);
          Result := SearchBuf(aString, Size, 0, 0, aSearchString, aSearchOptions)<>nil;
    End;
    

    and call it like this:

    ExistWordInString('Go Delphi Go','Delphi',[soWholeWord,soDown]);
    

    You may not fell any problem if you call it once. But if you call this in a loop (for example 1000 times or more) first using Pos function (like below) will surprisingly give you extra performance

    function ExistWordInString(const AString:string;const ASearchString:string;ASearchOptions: TStringSearchOptions): Boolean;
    var
      Size : Integer;
      AWChar: PWideChar;
    begin
       if Pos(LowerCase(ASearchString), LowerCase(AString)) = 0 then
       begin
          Exit(False);
       end;
    
       AWChar := PWideChar(AString);
       Size:=StrLen(AWChar);
       Result := SearchBuf(AWChar, Size, 0, 0, ASearchString, ASearchOptions)<>nil;
    end;
    

提交回复
热议问题