Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set Of String??!!

You are familiar with this block:

Var
  mySet: Set Of Char;
  C: Char;
begin
  mySet := ['a', 'b', 'c'];
  If C In mySet Then ShowMessage('Exists');
end;

Is there any way to declare Set Of STRING? or is there similar code that i can use instead? The important part of this block is If C In mySet Then ShowMessage('Exists'); I wanna use something like this about a set of string.
Thanks.

like image 511
Armin Taghavizad Avatar asked Jun 30 '10 15:06

Armin Taghavizad


3 Answers

Sets are implemented using bit arrays. So no, you cannot have a 'set of string'. Use a TStringList instead, ie:

var 
  mySet: TStringList;
  S: String;
begin 
  S := ...;
  mySet := TStringList.Create;
  try
    mySet.Add('a');
    mySet.Add('b');
    mySet.Add('c'); 
    if mySet.IndexOf(S) <> -1 Then ShowMessage('Exists');
  finally
    mySet.Free;
  end;
end; 
like image 191
Remy Lebeau Avatar answered Oct 14 '22 16:10

Remy Lebeau


The RTL System.StrUtils unit provides a very interesting method for this:

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

Use it like this:

  if MatchText(sLanguages, ['fr-FR', 'en-GB', 'de-DE', 'it-IT', 'fr-CH', 'es-ES']) then
    Writeln('found')
like image 40
Didier Cabalé Avatar answered Oct 14 '22 14:10

Didier Cabalé


You can make use of this.

type 
  TAnyEnum = (aeVal1, aeVal2, aeVal3);
  TEnuns = set of TAnyEnum;
  TAnyMessages: array [TAnyEnum] of String;

const 
  MyMessages: TAnyMessages = ('Exists', 'Something else', 'WTF!?');

var
  MySet : TEnums;
begin
  MySet = [aeVal1, aeVal2];
  If aeVal1 in MySet then ShowMessage(MyMessages[aeVal1]);
end;
like image 30
Fabricio Araujo Avatar answered Oct 14 '22 15:10

Fabricio Araujo