views:

897

answers:

4

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.

+1  A: 

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.

Burkhard
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;
Fabio Gomes
It's hard beeing a n00b and not being able do edit stuff :(
Fabio Gomes
+4  A: 

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;
gabr
+13  A: 

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

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

Re0sless
Thanks, didn't know that function exists at all!
gabr
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.
Fabio Gomes
I also had never come across that function. Thanks.
Richard A
Cool functions, thanks
Mohammed Nasman
I didn't know about MatchStr. Thanks for that.
Luke CK
A: 

Another possibility would be to concatenate all the strings into one string with a delimiter of some sort and use the pos command to see if its present. Useful if you don't know ahead of time the strings that you will be checking on as you can build the matchstr easily in code.

function DoesThisMatch(This:String):boolean;
var
  MatchStrs : String = 'ONE#TWO#THREE#FOUR#'; // note extra delimiter at the end
begin
  Result := POS(uppercase(This)+'#',MatchStrs) > 0;
end;
skamradt