Delphi 中是否有高效的全词搜索功能? [英] Is There An Efficient Whole Word Search Function in Delphi?

查看:11
本文介绍了Delphi 中是否有高效的全词搜索功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 provide the delimiters that define the word, e.g.:

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"在字符串中,我只需要它来返回真或假值.

I only need this to return a true or false value if the "Word" is in the string.

某处肯定有一些东西,因为标准的查找"对话框将仅匹配整个单词"作为其选项之一.

There must be something for this somewhere, because the standard Find Dialog has "Match whole word only" as one of it's options.

这通常(或最好)是如何实施的?

How is this normally (or best) implemented?

结论:

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

RRUZ's answer was perfect. The SearchBuf routine was just what I needed. I can even go into the StrUtils routine, extract the code, and modify it to fit my requirements.

我惊讶地发现 SearchBuf 不会先搜索单词,然后再检查分隔符.相反,它一次一个地遍历字符串的字符以寻找分隔符.如果找到,则检查字符串和另一个定界符.如果没有找到,它就会寻找另一个分隔符.为了效率,这很聪明!

I was surprised to find that SearchBuf doesn't first search for the word and then check for delimiters. Instead it goes through the characters of the string one at a time looking for a delimiter. If it finds one, then it checks for the string and another delimiter. If it doesn't find it, it then looks for another delimiter. For efficiency's sake, that's very smart!

推荐答案

您可以使用 SearchBuf 函数.

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 中是否有高效的全词搜索功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆