Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Search a String array in Delphi

Tags:

delphi

Is there a function in the Delphi standard library to search string arrays for a particular value?

e.g.

someArray:=TArray<string>.Create('One','Two','Three');
if ArrayContains(someArray, 'Two') then
    ShowMessage('It contains Two');
like image 434
awmross Avatar asked Dec 07 '11 00:12

awmross


1 Answers

There is absolutely no need to reinvent the wheel. StrUtils.MatchStr does the job.

procedure TForm1.FormCreate(Sender: TObject);
var
  someArray: TArray<string>;
begin
  someArray:=TArray<string>.Create('One','Two','Three');
  if MatchStr('Two', someArray) then
    ShowMessage('It contains Two');
end;

Note the parameter order convention.

Another note: MatchStr is a canonicalized name assigned to this function somewhen in between Delphi 7 and Delphi 2007. Historical name is AnsiMatchStr (convention is the same as in the rest of RTL: Str/Text suffix for case-sensitivity, Ansi prefix for MBCS/Locale)

like image 167
OnTheFly Avatar answered Sep 21 '22 11:09

OnTheFly