在 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”在字符串中,我只需要它来返回真值或假值。
某处必须有一些东西,因为标准的“查找”对话框将“仅匹配整个单词”作为其选项之一。
这通常(或最好)如何实施?
结论:
RRUZ 的回答很完美。SearchBuf 例程正是我所需要的。我什至可以进入 StrUtils 例程,提取代码并对其进行修改以满足我的要求。
我惊讶地发现 SearchBuf 不会先搜索单词然后再检查分隔符。相反,它一次一个地遍历字符串的字符来寻找分隔符。如果找到,则检查字符串和另一个分隔符。如果它没有找到它,它会寻找另一个分隔符。为了效率,这很聪明!