How to briefly check if a string is equal to any of several values?

Currently, I have one record (with different values) and three user constants with specific assigned values ​​(e.g. names, etc.).

I can compare the edit box with one user as follows:

if edit1.text = user1 
 then xxxx

All this is good, but how can I indicate that the edit box should check between three different users?

Example:

if edit1.text = user1 to user3
 then xxxx

How should I do it?

+3
source share
4 answers

Delphi does not support the use of strings in statements case, so you need to do this in a complicated way.

 if ((user1.name = edit1.text) and (user1.surname = edit2.text)) or 
    ((user2.name = edit1.text) and (user2.surname = edit2.text)) or 
    ((user3.name = edit1.text) and (user3.surname = edit2.text)) 
   then xxxx
+1
source

In recent versions of Delphi (I use XE) there is a StrUtils.pas block that contains

function MatchText(const AText: string; const AValues: array of string): Boolean;
function MatchStr(const AText: string; const AValues: array of string): Boolean;

MatchStr - , .

:

if MatchStr(edit1.text, [user1, user2, user3])
  then xxxx
+7

AnsiMatchStr()/AnsiMatchText(), , . AnsiIndexStr()/AnsiIndexText() .

+3

, , TStringList, " " if (x=a1) or (x=a2) or (x=a3).... :

 // FAcceptableValues is TStringList I set up elsewhere, such as my class constructor.
 if FAcceptableValues.IndexOf(x)>=0 then ...

. :

 var 
   Users:TList<TUser>;
   Edits:TList<TEdit>;
 begin
    ... other stuff like setup of FUsers/FEdits.
    if Match(Users,Edits) then ... 

:

 For U in Users do 
     for E in Edits do
          if U.Text=E.Text then 
            begin 
             result := true;
             exit
            end;
+2
source

All Articles