加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

delphi – 最好的方法来找到一个字符串是否在一个列表(没有泛型)

发布时间:2020-12-15 10:19:46 所属栏目:大数据 来源:网络整理
导读:我想做这样的事情: Result = 'MyString' in [string1,string2,string3,string4]; 这不能用于字符串,我不想这样做: Result = (('MyString' = string1) or ('MyString' = string2)); 另外我认为,创建一个StringList来做这只是太复杂。 有没有其他方法来实
我想做这样的事情:
Result = 'MyString' in [string1,string2,string3,string4];

这不能用于字符串,我不想这样做:

Result = (('MyString' = string1) or ('MyString' = string2));

另外我认为,创建一个StringList来做这只是太复杂。

有没有其他方法来实现这一点?

谢谢。

解决方法

你可以使用AnsiIndexText(const AnsiString AText,const数组的字符串AValues):integer或MatchStr(const AText:string; const AValues:string of string):Boolean;

就像是

Result := (AnsiIndexText('Hi',['Hello','Hi','Foo','Bar']) > -1);

要么

Result := MatchStr('Hi',['foo','Bar']);

AnsiIndexText returns the 0-offset
index of the first string it finds in
AValues that matches AText
case-insensitively. If the string
specified by AText does not have a
(possibly case-insensitive) match in
AValues,AnsiIndexText returns –1.
Comparisons are based on the current
system locale.

MatchStr determines if any of the
strings in the array AValues match the
string specified by AText using a case
sensitive comparison
. It returns true
if at least one of the strings in the
array match,or false if none of the
strings match.

注意AnsiIndexText不区分大小写,MatchStr是区分大小写,所以我想它取决于你的使用

编辑:2011-09-3:刚刚发现这个答案,并想到我会添加一个注释,在Delphi 2010中还有一个MatchText函数,它与MatchStr相同,但case insenstive。 – 拉里

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读