32

I want do something like this:

Result = 'MyString' in [string1, string2, string3, string4];

This can't be used with strings and I don't want to do something like this:

Result = (('MyString' = string1) or ('MyString' = string2));

Also I think that creating a StringList to do just this is too complex.

Is there some other way to achieve this?

Thanks.

3 Answers 3

72

You could use AnsiIndexText(const AnsiString AText, const array of string AValues):integer or MatchStr(const AText: string; const AValues: array of string): Boolean; (Both from StrUtils unit)

Something like:

Result := (AnsiIndexText('Hi',['Hello','Hi','Foo','Bar']) > -1);

or

Result := MatchStr('Hi', ['foo', 'Bar']); 

AnsiIndexText returns the 0-offset index of the first string it finds in AValues that matches AText case-insensitively. If the string specified by AText does not have a (possibly case-insensitive) match in AValues, AnsiIndexText returns –1. Comparisons are based on the current system locale.

MatchStr determines if any of the strings in the array AValues match the string specified by AText using a case sensitive comparison. It returns true if at least one of the strings in the array match, or false if none of the strings match.

Note AnsiIndexText has case-insensitively and MatchStr is case sensitive so I guess it depends on your use

EDIT: 2011-09-3: Just found this answer and thought I would add a note that, in Delphi 2010 there is also a MatchText function which is the same as MatchStr but case insenstive. -- Larry

5
  • Actually there is a better one, just searched a little in the StrUtils.pas and found the MatchStr which returns a Boolean: Result := MatchStr('Hi', ['foo', 'Bar']); Please add it to your answer. Commented Oct 29, 2008 at 13:48
  • MatchStr and MatchText are also available in Delphi 2007. Commented Oct 3, 2011 at 15:04
  • any equivalent for delphi 7 ?
    – CyprUS
    Commented May 3, 2012 at 11:26
  • 1
    @JerryGagnon: In which unit? I can't find them in SysUtils
    – Fabrizio
    Commented Feb 26, 2019 at 16:55
  • 1
    @Fabrizio they are in StrUtils
    – Re0sless
    Commented Feb 27, 2019 at 12:38
9

The code by Burkhard works, but iterates needlessly over the list even if a match is found.

Better approach:

function StringInArray(const Value: string; Strings: array of string): Boolean;
var I: Integer;
begin
  Result := True;
  for I := Low(Strings) to High(Strings) do
    if Strings[i] = Value then Exit;
  Result := False;
end;
2

Here is a function that does the job:

function StringInArray(Value: string; Strings: array of string): Boolean;
var I: Integer;
begin
  Result := False;
  for I := Low(Strings) to High(Strings) do
  Result := Result or (Value = Strings[I]);
end;

In fact, you do compare MyString with each string in Strings. As soon as you find one matching you can exit the for loop.

1
  • This works, please update your code with the Delphi one: function StringInArray(Value: string; Strings: array of string): Boolean; var I: Integer; begin Result := False; for I := Low(Strings) to High(Strings) do Result := Result or (Value = Strings[I]); end; Commented Oct 29, 2008 at 12:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.