Detecter deux lignes égales dans TStrings

Bacterius Messages postés 3792 Date d'inscription samedi 22 décembre 2007 Statut Membre Dernière intervention 3 juin 2016 - 17 févr. 2008 à 12:45
f0xi Messages postés 4205 Date d'inscription samedi 16 octobre 2004 Statut Modérateur Dernière intervention 12 mars 2022 - 17 févr. 2008 à 13:44
Bonjour, je voudrais juste savoir si une fonction est déja existante pour savoir si il y a 2 chaines d'un meme texte dans une liste de chaines (TStrings) et si possible détecter leur position, et sinon me donner quelques conseils pour créer cette fonction...

Merci, a bientôt...

1 réponse

f0xi Messages postés 4205 Date d'inscription samedi 16 octobre 2004 Statut Modérateur Dernière intervention 12 mars 2022 35
17 févr. 2008 à 13:44
type
  TIndexArray = array of integer;

function GetStrIndexs(const S: string; Strings: TStrings; var Count: integer; var Indexs: TIndexArray;
         const MatchCase: boolean = true): boolean;
var N : integer;
begin
  count := 0;
  result:= false;
  if Strings.Count <= 1 then
    exit;
  SetLength(Indexs, Strings.Count);
  case MatchCase of
    true  : for N := 0 to Strings.Count-1 do
              if CompareStr(S, Strings[N]) = 0 then
              begin
                Indexs[Count] := N;
                Count         := Count + 1;
              end;
    false : for N := 0 to Strings.Count-1 do
              if CompareText(S, Strings[N]) = 0 then
              begin
                Indexs[Count] := N;
                Count         := Count + 1;
              end;
  end;
  SetLength(Indexs, Count);
  result := Count > 0;
end;





EXEMPLE :







procedure TForm1.Button1Click(Sender: TObject);
var Idxs : TIndexArray;
    N,Cnt : integer;
begin
  if GetStrIndexs(Edit1.Text, ListBox1.Items, Cnt, Idxs, false) then
  begin
    ListBox1.MultiSelect := true;
    for N := 0 to ListBox1.Count-1 do
      ListBox1.Selected[ N ] := false;

    for N := 0 to Cnt-1 do
      ListBox1.Selected[ Idxs[N] ] := true;
  end;
end;




<hr size="2" width="100%" />


http://deefaze.gnomz.com
0
Rejoignez-nous