查找字符串是否在列表中的最佳方法(不用泛型) [英] Best way to find if a string is in a list (without generics)

查看:144
本文介绍了查找字符串是否在列表中的最佳方法(不用泛型)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做这样的事情:

Result = 'MyString' in [string1, string2, string3, string4];

这不能与字符串一起使用,我不想这样做: / p>

This can't be used with strings and I don't want to do something like this:

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

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

Also I think that creating a StringList to do just this is too complex.

有没有办法实现这一点?

Is there some other way to achieve this?

谢谢。

推荐答案

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

You could use AnsiIndexText(const AnsiString AText, const array of string AValues):integer or MatchStr(const AText: string; const AValues: array of string): Boolean;

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

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




AnsiIndexText返回第一个的0-offset
索引在
中找到的字符串匹配AText
case-insensitively 的AValues。如果由AText指定的字符串

AValues中没有
(可能不区分大小写)匹配,则AnsiIndexText返回-1。
比较是基于当前的
系统区域设置。

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确定数组AValues中的
字符串中是否匹配$由AText指定的b $ b字符串使用 case
敏感比较
。如果
数组中的至少一个字符串匹配,则返回true
,否则,如果
字符串中没有一个匹配,则返回false。

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区分大小写,所以我想这取决于你的使用

Note AnsiIndexText has case-insensitively and MatchStr is case sensitive so i guess it depends on your use

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

2011-09-3: Just found this answer and thought I would add a note that, in Delphi 2010 there is also a MatchText function which is the same as MatchStr but case insenstive. -- Larry

这篇关于查找字符串是否在列表中的最佳方法(不用泛型)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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