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

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

问题描述

在Delphi 2009或更高版本(Unicode)中,是否有任何内置函数或小例程写在某个地方,会执行一个相当有效的全字搜索,您可以在其中提供定义词的分隔符,例如:

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

const {Delim保存单词两边的分隔符}
Delim ='。;,:(){}/ \<>! '#$ 91#$ 92#$ 93#$ 94' - + * ='#$ A0#$ 84;

其中:

  Word:string; {是要搜索的Unicode字符串} 
Str:string; {is要搜索的Unicode字符串}

只有这样才能返回true或false值在字符串中。



必须有一些东西在这个地方,因为标准的查找对话框已经匹配整个单词作为其中一个选项。



这是如何实现的(或最好的)?






结论:



RRUZ的答案是完美的,SearchBuf例程就是我需要的,甚至可以进入StrUtils例程,提取代码,并修改它以符合我的要求。



我很惊讶,发现SearchBuf不会首先搜索f或单词然后检查分隔符。而是一次查找字符串的字符,寻找一个分隔符。如果找到一个,则它检查字符串和另一个分隔符。如果没有找到它,它会寻找另一个分隔符。为了效率,这很聪明!

解决方案

您可以使用 SearchBuf 功能与[soWholeWord]选项。

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

看到这个例子

 函数ExistWordInString(aString:PWideChar; aSearchString:string; aSearchOptions:TStringSearchOptions):Boolean; 
var
大小:整数;
开始
大小:= StrLen(aString);
结果:= SearchBuf(aString,Size,0,0,aSearchString,aSearchOptions)<> nil;
结束;

以这种方式使用

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

再见。


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;

where:

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

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?


Conclusion:

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.

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!

解决方案

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.

这篇关于在Delphi中是否有全面的Word搜索功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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