max*_*fax 17 arrays delphi string
我有这个代码:
var
ExtString: string;
const
Extensions : array[0..4] of string = ('.rar', '.zip', '.doc', '.jpg', '.gif');
if ExtString in Extensions then
Run Code Online (Sandbox Code Playgroud)
在最后一行,我收到一个错误:
[DCC错误] E2015运算符('then')不适用于此操作数类型
我想我不能这样做,所以我怎样才能正确执行我的任务呢?
Rob*_*ove 23
正如您所发现的那样,使用时无法检查字符串数组中的字符串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;
Run Code Online (Sandbox Code Playgroud)
你可以这样称呼它.
if StrInArray(ExtString,Extensions) then
在StrUtils.pas这个已经定义了.
function MatchStr(const AText: string; const AValues: array of string): Boolean;
Run Code Online (Sandbox Code Playgroud)
从常量数组初始化TStringList实例并使用IndexOf().
您可以使用函数IndexStr(区分大小写)或IndexText(不区分大小写)来System.StrUtils查找数组内的字符串并检索索引。
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');
Run Code Online (Sandbox Code Playgroud)
来自 embarcadero 的 docwiki 帮助链接。