Delphi 德尔菲:TStringList。包含什么?

Delphi 德尔菲:TStringList。包含什么?,delphi,find,compare,tstringlist,Delphi,Find,Compare,Tstringlist,Delphi2007中是否有任何集成的解决方案来检查TStringList是否包含某个值的一部分 e、 g: List.AddObject('This is a string', customStringObject1); List.AddObject('This is a mushroom', customStringObject2); List.AddObject('Random stuff', customStringObject3); 搜索“This is a”应该会给我带来“t

Delphi2007中是否有任何集成的解决方案来检查TStringList是否包含某个值的一部分

e、 g:

List.AddObject('This is a string', customStringObject1); 
List.AddObject('This is a mushroom', customStringObject2); 
List.AddObject('Random stuff', customStringObject3); 
搜索“This is a”应该会给我带来“true”,因为前两个元素部分地包含了这一点

到目前为止,我所知道的唯一方法是
TStringList.find(string,integer)
,但这会执行完整的字符串比较,也就是说,仅搜索这是一个字符串才会返回true


有什么建议吗?

未集成,但您可以在文本属性上使用Pos功能:

Pos('This is a', List.Text)

如果您想将其集成,可以为tstring创建类帮助器。

不是直接创建的,不是。您必须:

1) 调用
Text
属性上的
Pos()
,如果字符串太多,则该属性无效

2) 手动循环浏览列表,对每个
字符串调用
Pos()
。效率更高,但编码也更多

3) 从
TStringList
派生一个新类,并重写它的虚拟
CompareStrings()
方法来比较字符串(默认实现简单调用
AnsiCompareStr()
AnsiCompareText()
,具体取决于
区分大小写的
属性)。如果找到匹配项,则返回0。然后,您可以使用
TStringList.Find()
方法,该方法在内部调用
CompareStrings()
(请小心,
TStringList.Sort()
,但如果您调用
TStringList.CustomSort()
,则可以避免这种情况)