Intervertir 2 strings

Résolu
cs_Christophe67 Messages postés 80 Date d'inscription lundi 2 juin 2003 Statut Membre Dernière intervention 15 juin 2012 - 28 oct. 2011 à 16:27
Bacterius Messages postés 3792 Date d'inscription samedi 22 décembre 2007 Statut Membre Dernière intervention 3 juin 2016 - 4 nov. 2011 à 07:45
Bonjour,

J'aimerai savoir s'il existe une fonction en Delphi qui permet d'intervertir 2 strings

String1 <==> String2

sans passer par un buffer

String1 ==> Buffer
String2 ==> String1
Buffer ==> String2

J'espère avoir été suffisamment explicite.

Par avance, merci

10 réponses

cs_MAURICIO Messages postés 2106 Date d'inscription mardi 10 décembre 2002 Statut Modérateur Dernière intervention 15 décembre 2014 5
28 oct. 2011 à 18:14
C' est simple:

procedure TPostIt.ImgFlecheHautClick(Sender: TObject);

  procedure EchangeString(Lbl1, Lbl2: TLabel);
  var Sauv: string;
  begin
    Sauv := lbl1.Caption;
    lbl1.Caption := lbl2.Caption;
    lbl2.Caption := Sauv;
  end;

var
  LabDate1, LabHeure1, LabMemo1, LabCurs1: TLabel;
  LabDate2, LabHeure2, LabMemo2, LabCurs2: TLabel;
begin
  if CursPos=1 then exit;
  LabDate1 := TLabel(FindComponent('Date'+IntToStr(CursPos)));
  LabDate2 := TLabel(FindComponent('Date'+IntToStr(CursPos-1)));

  EchangeString(LabDate1, LabDate2);
end; 


A+



Composants Cindy pour Delphi
Faites une donation.
3
dubois77 Messages postés 675 Date d'inscription jeudi 17 avril 2008 Statut Membre Dernière intervention 19 février 2019 14
28 oct. 2011 à 17:13
Bonjour
Tu peux la faire toi même :

procedure EchangeSTring(var S1; var s2);
var z:string;
begin
z := s1;
s1 := s2;
s2 := Z;
end;

exemple

toto := 'XXXXX';
tata := 'YYYYY';
EchangeString(toto,tata);




Dubois77
0
cs_Christophe67 Messages postés 80 Date d'inscription lundi 2 juin 2003 Statut Membre Dernière intervention 15 juin 2012
28 oct. 2011 à 17:35
Merci je vois le principe, mais comment l'adatpter a 2 TLabel.Caption pour un code du genre :

procedure TPostIt.ImgFlecheHautClick(Sender: TObject);
var
LabDate1, LabHeure1, LabMemo1, LabCurs1: TLabel;
LabDate2, LabHeure2, LabMemo2, LabCurs2: TLabel;
begin
if CursPos=1 then exit;
LabDate1 := TLabel(FindComponent('Date'+IntToStr(CursPos)));
LabDate2 := TLabel(FindComponent('Date'+IntToStr(CursPos-1)));
EchangeString(LabDate1.Caption,LabDate2.Caption);
end;

qui ne fonctionne pas dans mon exemple.

Merci
0
cs_Christophe67 Messages postés 80 Date d'inscription lundi 2 juin 2003 Statut Membre Dernière intervention 15 juin 2012
28 oct. 2011 à 18:23
merci de votre aide !
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
Cirec Messages postés 3833 Date d'inscription vendredi 23 juillet 2004 Statut Modérateur Dernière intervention 18 septembre 2022 50
28 oct. 2011 à 18:28
Salut,

ou encore pour un résultat plus large :

type
  THackControl = class(TControl);

procedure EchangeSTring(S1, s2: TControl);
var z:string;
begin
  z := THackControl(s1).Caption;
  THackControl(s1).Caption := THackControl(s2).Caption;
  THackControl(s2).Caption := Z;
end;

procedure TForm3.Button4Click(Sender: TObject);
begin
  EchangeSTring(Label1, Button4);
end;


ce code ne se limite pas qu'aux TLabel mais fonctionne avec tout composant qui possède un Caption ou non à condition qu'il descende de TControl


[hr]@+Cirec
[hr]
0
cs_Christophe67 Messages postés 80 Date d'inscription lundi 2 juin 2003 Statut Membre Dernière intervention 15 juin 2012
28 oct. 2011 à 19:12
En effet, plus pratique car plus générale mais me cause un tas d'erreur quand je déclare le contrôle dans Type.
Je conserve donc pour le moment la réponse de Mauricio que j'ai adapté sans soucis.
0
Cirec Messages postés 3833 Date d'inscription vendredi 23 juillet 2004 Statut Modérateur Dernière intervention 18 septembre 2022 50
28 oct. 2011 à 19:33
je ne comprend pas bien le problème !

c'est ça qui cloche ?
type
  THackControl = class(TControl);

ou tu le déclares autrement ?

[hr]@+Cirec
[hr]
0
Caribensila Messages postés 2527 Date d'inscription jeudi 15 janvier 2004 Statut Membre Dernière intervention 16 octobre 2019 18
29 oct. 2011 à 04:46
Salut,

Le code de Cirec fonctionne super bien !

Son seul problème est qu'il ne répond pas à la question de Christophe :

J'aimerai savoir s'il existe une fonction en Delphi qui permet d'intervertir 2 strings sans passer par un buffer



procedure EchangeSTringWithoutBuffer(S1, S2: TControl);
begin
  THackControl(S1).Caption := THackControl(S1).Caption+THackControl(S2).Caption;
  THackControl(S2).Caption := Copy(THackControl(S1).Caption,0,Length(THackControl(S1).Caption)-Length(THackControl(S2).Caption));
  THackControl(S1).Caption := Copy(THackControl(S1).Caption,Length(THackControl(S2).Caption)+1,Length(THackControl(S1).Caption)-Length(THackControl(S2).Caption));
end;


Mais c'est du chipotage de chez chipotage.
0
Bacterius Messages postés 3792 Date d'inscription samedi 22 décembre 2007 Statut Membre Dernière intervention 3 juin 2016 10
4 nov. 2011 à 07:44
Bah sinon y'a StrUtils.ReverseString (aussi)
0
Bacterius Messages postés 3792 Date d'inscription samedi 22 décembre 2007 Statut Membre Dernière intervention 3 juin 2016 10
4 nov. 2011 à 07:45
Ah désolé oublie ca.

Par contre si ce que tu voulais c'est echanger deux strings sans jamais passer par une variable intermédiaire (quelque soit sa taille, par exemple pour un exercice ou quelque chose) tu peux utiliser un xorswap. Pas besoin de passer par un buffer intermédiaire. Mais avec les machines d'aujourd'hui c'est plus simple d'utiliser un buffer.
0
Rejoignez-nous