23

I have this code:

var
  ExtString: string;
const
  Extensions : array[0..4] of string = ('.rar', '.zip', '.doc', '.jpg', '.gif');

if ExtString in Extensions then

On the last line, I get an error:

[DCC Error] E2015 Operator ('then') not applicable to this operand type

I think I can not do this, so how can I properly perform my task?

Rob Kennedy
  • 161,384
  • 21
  • 275
  • 467
maxfax
  • 4,281
  • 12
  • 74
  • 120
  • **in** operator is not applicable to these operand types – Premature Optimization Jun 12 '11 at 04:07
  • 1
    This is an explanation why the OP's attempt did not work, but it is no real answer since the question was "so how can i properly perform this task?" – jpfollenius Jun 12 '11 at 10:01
  • Possible duplicate of [Best way to find if a string is in a list (without generics)](http://stackoverflow.com/questions/246623/best-way-to-find-if-a-string-is-in-a-list-without-generics) – Jay Elston Dec 01 '16 at 22:25
  • Use AnsiIndexStr() or AnsiIndexText(). See [Best way to find if a string is in a list (without generics)](http://stackoverflow.com/questions/246623/best-way-to-find-if-a-string-is-in-a-list-without-generics). – Jay Elston Dec 01 '16 at 22:27

3 Answers3

30

As you have found you can't check for a String in an Array of String, using in.

You could use this function instead of the if statement.

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;

You can call it like this.

if StrInArray(ExtString,Extensions) then

The StrUtils.pas has this already defined.

function MatchStr(const AText: string; const AValues: array of string): Boolean; 
Robert Love
  • 12,447
  • 2
  • 48
  • 80
  • 8
    +1 for MatchStr although it is unfortunately named. It should be a noun rather than a verb. I must say I am not a fan of the new exit syntax. But if you are going to use it shouldn't you use it consistently. Using result and exit() in the same routine makes it harder to parse. – David Heffernan Jun 12 '11 at 06:15
  • 4
    Be careful about case-sensitivity. The file system is case-insensitive and you can find plenty of files with the .JPG extension. The code above will miss this. – Misha Jun 12 '11 at 09:10
  • 4
    @Misha: That's what `MatchText` is for. In some Delphi versions, it only exists as `AnsiMatchText` (and perhaps `WideMatchText` in `WideStrings.pas`). – afrazier Jun 15 '11 at 19:36
8

Initialise a TStringList instance from the constant array and use IndexOf().

Misha
  • 1,816
  • 1
  • 13
  • 16
  • 6
    too heavyweight, simple loop better – David Heffernan Jun 12 '11 at 06:07
  • 3
    Not if it is done frequently and the TStringList is persistent. Who know what else this might be used for? – Misha Jun 12 '11 at 06:30
  • 3
    Make the string list persistent and you abandon thread safety. Even so you have to assign to the string list which is pointless. Loop is better no question. String list is sledgehammer to crack nut. – David Heffernan Jun 12 '11 at 06:39
  • For only 4 strings it is probably overkill. However, thread-safety is not an issue because the string list can be initialised once and after that it is read-only. – Misha Jun 12 '11 at 08:02
  • 3
    TStringList.IndexOf is not heavy-weight. And a simple for loop is NOT better. – Warren P Jun 12 '11 at 15:01
  • @David: There's a tradeoff to be made. TStringList also comes with (quick-)sorting and binary searches from the VCL. – afrazier Jun 15 '11 at 19:43
  • @DavidHeffernan you can make your global stringlist threadvar so that every thread will have it's own instance. That way it will be safe(r) and persistent. – nurettin May 18 '18 at 06:59
  • @DavidHeffernan at least we agree on one thing. – nurettin May 18 '18 at 08:58
7

You can use the funcions IndexStr (case sensitive) or IndexText (case insensitive) from System.StrUtils to find the string inside the array and retrive the index.

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');

Link to help in docwiki from embarcadero.