Is There An Efficient Whole Word Search Function in Delphi?

后端 未结 4 1400
灰色年华
灰色年华 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:40

    Just because Delphi's editor has a "word match" function, that doesn't mean the Delphi library offers it!

    Typically, in most languages the way to go for this is a regular expression. Seems they're (still) not built into Delphi, as there are 3rd party libraries offering the capability. The first example I found is: http://delphi.about.com/od/toppicks/tp/delphi-regular-expressions.htm .

    Typically, you'd build a regular expression something like

    myRegex = '[' + Delim + ']+' + Word + '[' + Delim + ']+';
    if regexSearch (Str, myRegex) then ...
    

    You'll want to get details from the documentation of the library you get. My example doesn't correctly handle the case of the word starting at the beginning of Str or ending at its end, or being all of Str.

    0 讨论(0)
  • 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;
    
    0 讨论(0)
  • 2020-12-06 07:45

    This function is not exactly what you need but it is pretty close:

    I hope it is useful:

    { Copy all whole words from MainStr. The result will not have more than MaxChars characters. }
    
    function CopyWords(MainStr: string; MaxChars: Integer): string;   
    VAR EndsInSpace: Boolean;
        EndString, i: Integer;
        NextChar: char;
    begin
     Assert(MaxChars > 0);
     EndString:= MaxChars;
    
     if Length(MainStr) > MaxChars then
      begin
       NextChar:= mainstr[MaxChars+1];
    
       if (MainStr[MaxChars] <> ' ') AND (NextChar <> ' ')
       then
         begin
          for i:= MaxChars downto 1 DO
           if MainStr[i]= ' ' then
            begin
             EndString:= i;
             Break;
            end
         end
       else
        if (MainStr[MaxChars]  = ' ')
        OR (MainStr[MaxChars] <> ' ') AND (NextChar = ' ')
        then EndString:= MaxChars;
      end;
    
     Result:= CopyTo(MainStr, 1, EndString);
     Result:= TrimRight(Result);
    end;
    
    0 讨论(0)
  • 2020-12-06 08:04

    You can use the SearchBuf function with the [soWholeWord] option.

    function SearchBuf(Buf: PAnsiChar; BufLen: Integer; SelStart: Integer; SelLength: Integer; SearchString: AnsiString; Options: TStringSearchOptions): PAnsiChar;
    

    See this example

    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;
    

    Use it this way

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

    Bye.

    0 讨论(0)
提交回复
热议问题