cs_MAURICIO
Messages postés2106Date d'inscriptionmardi 10 décembre 2002StatutModérateurDernière intervention15 décembre 2014
-
22 juin 2004 à 17:51
JulioDelphi
Messages postés2226Date d'inscriptiondimanche 5 octobre 2003StatutMembreDernière intervention18 novembre 2010
-
8 déc. 2004 à 16:45
Cette discussion concerne un article du site. Pour la consulter dans son contexte d'origine, cliquez sur le lien ci-dessous.
JulioDelphi
Messages postés2226Date d'inscriptiondimanche 5 octobre 2003StatutMembreDernière intervention18 novembre 201014 8 déc. 2004 à 16:45
voici la fonction Parse de StringManager :
function TStringManager.Parse(const DelimStr : String; var s : String; IgnoreDelimAtStart : Boolean = True): String;
var p : Integer;
begin
p := Pos(DelimStr, s);
if p=1 then if IgnoreDelimAtStart then
begin
s := System.Copy(s, p+Length(DelimStr), Length(s));
p := Pos(DelimStr, s);
end;
if p=0 then
begin
Result := s;
s := '';
Exit;
end;
Result := System.Copy(s, 1, p-1);
s := System.Copy(s, p+Length(DelimStr), Length(s));
end;
puis aussi avec un CharSet :
function TStringManager.Parse(const aSet: TCharSet; var s: String; IgnoreDelimAtStart : Boolean = True): String;
var p : Integer;
begin
p := Pos(aSet, s);
if p=1 then if IgnoreDelimAtStart then
begin
s := System.Copy(s, p+1, Length(s));
p := Pos(aSet, s);
end;
if p=0 then
begin
Result := s;
s := '';
Exit;
end;
Result := System.Copy(s, 1, p-1);
s := System.Copy(s, p+1, Length(s));
end;
et pour finir direct dans eun TStringList :
procedure TStringManager.Parse(const DelimStr : String; s : String; const aList : TStrings; MopAlso : Boolean True; IgnoreDelimAtStart : Boolean True);
begin
if aList=nil then DoRaise('aList=nil','Parse');
while s<>'' do
begin
if MopAlso
then aList.Add(Mop(Parse(DelimStr, s, IgnoreDelimAtStart)))
else aList.Add(Parse(DelimStr, s, IgnoreDelimAtStart));
end;
end;
Moi je pensait plus du cote de Mauricio avec les fonctions recursives voila ce que cela donne :
_______________________
procedure SeparerChaine(Chaine, Sep : string; Dans : TStrings);
var
Position : Integer;
begin
Position:=Pos(Sep,Chaine);
Dans.Add(Copy(Chaine,0,Position-1));
if Position <> 0 then
SeparerChaine(RightStr(Chaine,Length(Chaine)-Position),Sep,Dans)
else
Dans.Add(Chaine);
end;
_______________________
Voia c'est tres simple, très rapide et cela marche.
Apres si on veut compter le nombre de sous chaines, on peut mettre une variable comme Kenavo a fait et l'incrementer a chaque fois.
Mais il est bien plus intelligent d'utiliser la propriete Count du TStrings ou High(Result) pour la version de Kenavo.
cs_MAURICIO
Messages postés2106Date d'inscriptionmardi 10 décembre 2002StatutModérateurDernière intervention15 décembre 20145 22 juin 2004 à 18:41
On pourrait biensûr, cela va de soit, écrire une autre fonction pour changer les valeurs des sous-chaines mais bon, si qq' un a un idée...
cs_MAURICIO
Messages postés2106Date d'inscriptionmardi 10 décembre 2002StatutModérateurDernière intervention15 décembre 20145 22 juin 2004 à 17:51
Il serait plus intérssant (mais je ne sait pas comment faire) d' écrire une fonction dans le genre:
function STRING_SUBSTRING(Str: String; Separateur: Char; Position: Integer): String;
Exemple:
var SousChaine: String;
begin
SousChaine := STRING_SUBSTRING(maChaine, ';', 3);
Jusque là ça va mais moi, j' ai envie de pouvoir attribuer la valeur que je veux et à la position que je veux:
Exemple:
STRING_SUBSTRING(maChaine, ';', 3) := 'nouvelle valeur!!!';
8 déc. 2004 à 16:45
function TStringManager.Parse(const DelimStr : String; var s : String; IgnoreDelimAtStart : Boolean = True): String;
var p : Integer;
begin
p := Pos(DelimStr, s);
if p=1 then if IgnoreDelimAtStart then
begin
s := System.Copy(s, p+Length(DelimStr), Length(s));
p := Pos(DelimStr, s);
end;
if p=0 then
begin
Result := s;
s := '';
Exit;
end;
Result := System.Copy(s, 1, p-1);
s := System.Copy(s, p+Length(DelimStr), Length(s));
end;
puis aussi avec un CharSet :
function TStringManager.Parse(const aSet: TCharSet; var s: String; IgnoreDelimAtStart : Boolean = True): String;
var p : Integer;
begin
p := Pos(aSet, s);
if p=1 then if IgnoreDelimAtStart then
begin
s := System.Copy(s, p+1, Length(s));
p := Pos(aSet, s);
end;
if p=0 then
begin
Result := s;
s := '';
Exit;
end;
Result := System.Copy(s, 1, p-1);
s := System.Copy(s, p+1, Length(s));
end;
et pour finir direct dans eun TStringList :
procedure TStringManager.Parse(const DelimStr : String; s : String; const aList : TStrings; MopAlso : Boolean True; IgnoreDelimAtStart : Boolean True);
begin
if aList=nil then DoRaise('aList=nil','Parse');
while s<>'' do
begin
if MopAlso
then aList.Add(Mop(Parse(DelimStr, s, IgnoreDelimAtStart)))
else aList.Add(Parse(DelimStr, s, IgnoreDelimAtStart));
end;
end;
StrMan.pas dispo ici : http://diabloporc.Free.fr/delphi/strman/
8 déc. 2004 à 14:28
_______________________
procedure SeparerChaine(Chaine, Sep : string; Dans : TStrings);
var
Position : Integer;
begin
Position:=Pos(Sep,Chaine);
Dans.Add(Copy(Chaine,0,Position-1));
if Position <> 0 then
SeparerChaine(RightStr(Chaine,Length(Chaine)-Position),Sep,Dans)
else
Dans.Add(Chaine);
end;
_______________________
Voia c'est tres simple, très rapide et cela marche.
Apres si on veut compter le nombre de sous chaines, on peut mettre une variable comme Kenavo a fait et l'incrementer a chaque fois.
Mais il est bien plus intelligent d'utiliser la propriete Count du TStrings ou High(Result) pour la version de Kenavo.
22 juin 2004 à 18:41
22 juin 2004 à 17:51
function STRING_SUBSTRING(Str: String; Separateur: Char; Position: Integer): String;
Exemple:
var SousChaine: String;
begin
SousChaine := STRING_SUBSTRING(maChaine, ';', 3);
Jusque là ça va mais moi, j' ai envie de pouvoir attribuer la valeur que je veux et à la position que je veux:
Exemple:
STRING_SUBSTRING(maChaine, ';', 3) := 'nouvelle valeur!!!';
Si vous savez comment faire...