gpt4 book ai didi

delphi - Delphi中有高效的全词搜索功能吗?

转载 作者:行者123 更新时间:2023-12-03 14:49:47 27 4
gpt4 key购买 nike

在 Delphi 2009 或更高版本 (Unicode) 中,是否有任何内置函数或小例程编写在某处,可以在您提供定义单词的分隔符的情况下进行相当有效的全单词搜索,例如:

function ContainsWord(Word, Str: string): boolean;

const { Delim holds the delimiters that are on either side of the word }
Delim = ' .;,:(){}"/\<>!?[]'#$91#$92#$93#$94'-+*='#$A0#$84;

哪里:

Word: string;  { is the Unicode string to search for }
Str: string; { is the Unicode string to be searched }

如果“Word”在字符串中,我只需要它返回 true 或 false 值。

某个地方一定有这样的东西,因为标准查找对话框将“仅匹配整个单词”作为其选项之一。

通常(或最好)如何实现?

<小时/>

结论:

RRUZ 的回答很完美。 SearchBuf 例程正是我所需要的。我什至可以进入 StrUtils 例程,提取代码并修改它以满足我的要求。

我惊讶地发现 SearchBuf 并不是先搜索单词,然后检查分隔符。相反,它一次一个地遍历字符串的字符来查找分隔符。如果找到,则会检查字符串和另一个分隔符。如果没有找到,它就会寻找另一个分隔符。为了提高效率,这非常聪明!

最佳答案

您可以使用SearchBuf使用 [soWholeWord] 选项运行。

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

查看此示例

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;

这样使用

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

再见。

关于delphi - Delphi中有高效的全词搜索功能吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1678572/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com