Is There An Efficient Whole Word Search Function in Delphi?

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

提交回复
热议问题