IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Delphi Discussion :

Spliter un string en array of char ?


Sujet :

Delphi

  1. #1
    Membre actif

    Homme Profil pro
    Inscrit en
    Septembre 2002
    Messages
    472
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 40
    Localisation : France

    Informations forums :
    Inscription : Septembre 2002
    Messages : 472
    Points : 262
    Points
    262
    Par défaut Spliter un string en array of char ?
    Bonjour,

    J'ai une chaine de caractère du style :
    var
    MaChaine : string;
    MonTab : array of char;
    begin
    MaChaine := '123 456 789 0AB CDEF'; {ma string représente des caractères hexadécimaux}
    Je voudrais splitter cette chaine grâce aux espaces en un tableau de char : MonTab := {123,456,789,0AB,CDEF}

    Comment puis-je faire ?

    Merci d'avance,
    MaTHieU_
    Embarcadero RAD Studio XE / Microsoft Windows 7 Édition Intégrale (64 bits)

  2. #2
    Membre du Club
    Inscrit en
    Février 2005
    Messages
    242
    Détails du profil
    Informations personnelles :
    Âge : 32

    Informations forums :
    Inscription : Février 2005
    Messages : 242
    Points : 63
    Points
    63
    Par défaut
    Et pourquoi veux-tu faire ça ?

    Tu sais en Delphi, le type String et plus ou moins un array of Char, ainsi tu peux accéder au caractère d'une chaîne en faisant
    MaChaine[n];

    ce qui récupère le Neme caractère de la chaîne MaChaine.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    var
      MaChaine: String;
    begin
      MaChaine:='Ceci est une chaîne !';
      ShowMessage(MaChaine[4]); // affiche 'i'
     
      MaChaine[3]:='l';
      MaChaine[4]:='a';
      ShowMessage(MaChaine); // affiche 'Cela est une chaîne !'
    end;
    Donc je voit pas l'intérêt de faire cet conversion. Enfin, sache que le premier carctère d'une chaîne a pour indice 1.

  3. #3
    Membre actif

    Homme Profil pro
    Inscrit en
    Septembre 2002
    Messages
    472
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 40
    Localisation : France

    Informations forums :
    Inscription : Septembre 2002
    Messages : 472
    Points : 262
    Points
    262
    Par défaut
    Salut,

    En fait, le code hexa est donné via un TEdit.

    Je dois donc decouper ma chaine de caractères en un tableau de char ou même de integer car après je dosi utiliser chaque bout comme chiffre.

    MaTHieU_
    Embarcadero RAD Studio XE / Microsoft Windows 7 Édition Intégrale (64 bits)

  4. #4
    Membre confirmé

    Homme Profil pro
    Chef de Projet ATIC
    Inscrit en
    Novembre 2005
    Messages
    274
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 49
    Localisation : France, Eure (Haute Normandie)

    Informations professionnelles :
    Activité : Chef de Projet ATIC
    Secteur : Finance

    Informations forums :
    Inscription : Novembre 2005
    Messages : 274
    Points : 508
    Points
    508
    Par défaut
    Voici un exemple pour un tableau de Integer :

    Avec un While :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    procedure TForm1.Button1Click(Sender: TObject);
    var
      MaChaine : string;
      MonTab : array of integer;
      I, iCount : Integer;
    begin
      MaChaine := '123 456 789 0AB CDEF';
      iCount := 1;
      I := pos(' ', Machaine); {Position du 1er espace}
      While I <> 0 do
      begin
        SetLength(MonTab, iCount); {Taille tableau }
        MonTab[icount - 1] := StrToInt('$' + LeftStr(MaChaine, I - 1)); {Conversion Hexa}
        MaChaine := Copy(MaChaine, I + 1, Length(MaChaine) - I); {Reste de la chaine}
        I := pos(' ', Machaine);
        inc(iCount);
      end;
      SetLength(MonTab, iCount); {Derniere partie}
      MonTab[icount - 1] := StrToInt('$' + MaChaine);
    end;
    Avec un Repeat...Until

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    procedure TForm1.Button1Click(Sender: TObject);
    var
      MaChaine : string;
      MonTab : array of integer;
      I, iCount : Integer;
    begin
      MaChaine := '123 456 789 0AB CDEF';
      iCount := 1;
      repeat
        I := pos(' ', Machaine);
        SetLength(MonTab, iCount);
        if I > 0 then
        begin
          MonTab[icount - 1] := StrToInt('$' + LeftStr(MaChaine, I - 1));
          MaChaine := Copy(MaChaine, I + 1, Length(MaChaine) - I);
          inc(iCount);
        end
        else
          MonTab[icount - 1] := StrToInt('$' + MaChaine);
      until I = 0;
    end;

  5. #5
    Membre actif

    Homme Profil pro
    Inscrit en
    Septembre 2002
    Messages
    472
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 40
    Localisation : France

    Informations forums :
    Inscription : Septembre 2002
    Messages : 472
    Points : 262
    Points
    262
    Par défaut
    Bonsoir,

    Merci pour ce coup de pouce !

    MaTHieU_
    Embarcadero RAD Studio XE / Microsoft Windows 7 Édition Intégrale (64 bits)

  6. #6
    Membre confirmé

    Homme Profil pro
    Chef de Projet ATIC
    Inscrit en
    Novembre 2005
    Messages
    274
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 49
    Localisation : France, Eure (Haute Normandie)

    Informations professionnelles :
    Activité : Chef de Projet ATIC
    Secteur : Finance

    Informations forums :
    Inscription : Novembre 2005
    Messages : 274
    Points : 508
    Points
    508
    Par défaut
    Pas de quoi

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Réponses: 22
    Dernier message: 12/06/2007, 14h28
  2. Copy String to array of char
    Par Ardely dans le forum Delphi
    Réponses: 11
    Dernier message: 11/02/2007, 00h24
  3. Champ String et Constante Array of Char ?
    Par BXDSPORT dans le forum Delphi
    Réponses: 4
    Dernier message: 24/08/2006, 12h35
  4. TFileStream et Array of char
    Par busy999 dans le forum Langage
    Réponses: 2
    Dernier message: 06/07/2004, 21h00
  5. Réponses: 6
    Dernier message: 24/07/2003, 12h39

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo