我有这段代码:
var
ExtString: string;
const
Extensions : array[0..4] of string = ('.rar', '.zip', '.doc', '.jpg', '.gif');
if ExtString in Extensions then
在最后一行,我收到了一个错误:
[DCC Error] E2015 运算符 ('then') 对这个操作数类型不适用
我认为我不能这样做,那么我该如何正确地执行我的任务?
我有这段代码:
var
ExtString: string;
const
Extensions : array[0..4] of string = ('.rar', '.zip', '.doc', '.jpg', '.gif');
if ExtString in Extensions then
在最后一行,我收到了一个错误:
[DCC Error] E2015 运算符 ('then') 对这个操作数类型不适用
我认为我不能这样做,那么我该如何正确地执行我的任务?
正如您发现的那样,您无法使用in
在字符串数组中检查字符串。
您可以使用此函数代替if
语句。
function StrInArray(const Value : String;const ArrayOfString : Array of String) : Boolean;
var
Loop : String;
begin
for Loop in ArrayOfString do
begin
if Value = Loop then
begin
Exit(true);
end;
end;
result := false;
end;
你可以这样调用它。
if StrInArray(ExtString,Extensions) then
StrUtils.pas
已经定义了这个函数。
function MatchStr(const AText: string; const AValues: array of string): Boolean;
System.StrUtils
的函数 IndexStr
(区分大小写)或 IndexText
(不区分大小写)来查找数组内的字符串并检索索引。var
ExtString: string;
const
Extensions : array[0..4] of string = ('.rar', '.zip', '.doc', '.jpg', '.gif');
begin
if (IndexStr(ExtString, Extensions) <> -1) then
ShowMessage('Finded')
else
ShowMessage('Not finded');