Manipulation de StringList

Résolu
cs_lusitano69 Messages postés 21 Date d'inscription mardi 10 octobre 2006 Statut Membre Dernière intervention 26 février 2008 - 26 févr. 2008 à 01:18
cs_lusitano69 Messages postés 21 Date d'inscription mardi 10 octobre 2006 Statut Membre Dernière intervention 26 février 2008 - 26 févr. 2008 à 17:05
Bonjour j'ai fait un ptit programme de test pour comprendre les StringList qui pourrair bien me servir bientot.
Voici mon code:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type
  TForm1 = class(TForm)
    ListBox1: TListBox;
    Button1: TButton;
    Edit1: TEdit;
    Label1: TLabel;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Déclarations privées }
  public
    { Déclarations publiques }
  end;

var
  Form1: TForm1;
  StL:TStringList;
  Code_ascii : string;
implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
begin
  StL:=TStringList.Create;    //Créer le stringlist
  StL.LoadFromFile('Polices.txt');
  StL.Sort;
  ListBox1.Items.AddStrings(StL);
end;

procedure TForm1.Button1Click(Sender: TObject);
var
i,j : Integer;
Li : Integer;
test : integer;

begin
Label1.Caption := '';
Li := Length(Edit1.Text); //Taille du texte}/
if Li = 0 then
  Exit; //Si il y a rien on sort

for i := 1 to Li Do
  begin
    {Ord transforme un caractere en sa valeur ascii
    Edit1.Text[i]  est le caractere du edit à la position i}
    Label1.Caption := Label1.Caption + IntToStr(Ord(Edit1.Text[i]));
    Code_ascii := IntToStr(Ord(Edit1.Text[i]));
    ShowMessage(IntToStr(StL.IndexOf(Code_ascii)));
  end;
end;

Comprend pas pourquoi il me renvoi -1 à chaque fois pourtant les valeurs que j'ai dans la variable Code_ascii "logiquement" sont contenu dans mon Stringlist il devrai me retourner la position de la recherche de l'indice de ma chaine(Code_ascii)

4 réponses

f0xi Messages postés 4205 Date d'inscription samedi 16 octobre 2004 Statut Modérateur Dernière intervention 12 mars 2022 35
26 févr. 2008 à 03:23
procedure TForm1.FormCreate(Sender: TObject);
begin
  StL := TStringList.Create;
  StL.Sorted := true;
  StL.LoadFromFile(ExtractFilePath(Paramstr(0))+'Polices.txt');
  ListBox1.Items.Assign(StL);
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
   StL.Free; // on oublie pas de liberer StL
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  i, j, Li, Test : Integer;
begin
  Label1.Caption := '';
  Li := Length(Edit1.Text);
  if Li = 0 then
    Exit;

  for i := 1 to Li Do
  begin

    Code_ascii := IntToStr(Ord(Edit1.Text[i])); // a faire une fois

    Label1.Caption := Label1.Caption +
Code_ascii
;
    if StL.count > 0 then
      ShowMessage(IntToStr(StL.IndexOf(Code_ascii)));
  end;
end;




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


http://deefaze.gnomz.com
3
cs_lusitano69 Messages postés 21 Date d'inscription mardi 10 octobre 2006 Statut Membre Dernière intervention 26 février 2008
26 févr. 2008 à 08:17
Merci pour ton code F0xi mais toujours le même résultat il  me renvoi -1 a chaque fois
voici quelques lignes de mon fichier texte. je voudrais qu'il me renvoie la position de 32,97,65 etc etc selon se qu'on entre dans le Edit

32:[0,0]:5
97:[1,6][1,9][1,10][2,5][2,8][2,11][3,5][3,8][3,11][4,5][4,8][4,10][5,6][5,7][5,8][5,9][5,10][5,11]:5
65:[1,10][1,11][2,8][2,9][3,6][3,7][3,8][4,3][4,4][4,5][4,8][5,2][5,8][6,3][6,4][6,5][7,6][7,7][7,8][8,8][8,9][9,10][9,11]:9
98:[1,2][1,3][1,4][1,5][1,6][1,7][1,8][1,9][1,10][1,11][2,6][2,11][3,5][3,11][4,5][4,11][5,6][5,7][5,8][5,9][5,10]:5
66:[1,2][1,3][1,4][1,5][1,6][1,7][1,8][1,9][1,10][1,11][2,2][2,6][2,11][3,2][3,6][3,11][4,2][4,6][4,11][5,2][5,6][5,11][6,2][6,6][6,11][7,3][7,4][7,5][7,7][7,8][7,9][7,10]:7
99:[1,6][1,7][1,8][1,9][1,10][2,5][2,11][3,5][3,11][4,5][4,11][5,6][5,10]:5

etc.....
3
f0xi Messages postés 4205 Date d'inscription samedi 16 octobre 2004 Statut Modérateur Dernière intervention 12 mars 2022 35
26 févr. 2008 à 16:55
AH! c'est encore pour ce truc.

ne t'ais-je pas dis de ne pas travailler avec du texte ?
ça pourrie les performances de ton programme, et pas qu'un peu.

t'as choisis une methode qui au final te fait faire pleins de truc alambiqué et pas forcement approprié.

tu risque de jongler longtemps avec des pos, indexof, inttostr et autres. non franchement, c'est pas du tout comme ça qu'il aurait fallut faire.

<hr size="2" width="100%" />
http://deefaze.gnomz.com
0
cs_lusitano69 Messages postés 21 Date d'inscription mardi 10 octobre 2006 Statut Membre Dernière intervention 26 février 2008
26 févr. 2008 à 17:05
j'ai reussi a obtenir se que je voulais, J'ai laisser tomber le code du dessus et j ai procéder différement
0
Rejoignez-nous