How do I check whether a string exists in an array? How do I check whether a string exists in an array? arrays arrays

How do I check whether a string exists in an array?


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; 


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


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.