Spikto
Messages postés9Date d'inscriptionvendredi 9 juillet 2004StatutMembreDernière intervention27 octobre 2005
-
27 oct. 2005 à 11:53
Cirec
Messages postés3833Date d'inscriptionvendredi 23 juillet 2004StatutModérateurDernière intervention18 septembre 2022
-
28 oct. 2005 à 14:35
Bonjour j'ai une fonction qui permet de découper un variable de type texte mais j'ai des lettre qui disparaisse de temps en temps.
Voici ma fonction
type
TabString = Array of string ;
function Decoupe(s : string; Car : Char; nb : Integer; var lu : Integer) : TabString;
var
y, idx : Integer;
begin
idx :=0;
y :=99;
SetLength(Result,nb);
while (idx<nb) and (y>0) do
begin
y:=pos(Car,s);
if y=0 then
begin
Result[Idx]:=s;
inc(idx);
end
else
begin
Result[idx]:=copy(s,1,y-1);
inc(idx);
delete(s,1,y);
while (length(s)>0) and (s[y]=Car) do
delete(s,1,1);
end;
end;
lu:=idx;
end;
Est ce que vous pouvez me dire ce qui n'est pas correcte dans ma fonction ?
Cirec
Messages postés3833Date d'inscriptionvendredi 23 juillet 2004StatutModérateurDernière intervention18 septembre 202250 28 oct. 2005 à 14:35
Bonjour Spikto,
Voici la solution à ton problème:
function Decoupe(s : string; Car : Char; nb : Integer; var lu : Integer) : TabString;
var
y, idx : Integer;
begin
idx := 0;
y :=99;
SetLength(Result,nb);
while (idx<nb) and (y>0) do
begin
y:=pos(Car,s);
if y=0 then
begin
Result[Idx]:=s;
inc(idx);
end
else
begin
Result[idx]:=copy(s,1,y-1);
inc(idx);
delete(s,1,y);
while (length(s)>0) and (s[ 1 ] =Car) do // l'erreur était ici
delete(s,1,1);
end;
end;
lu:= idx;
end;
{****************************************************************}
Une alternative à ta fonction
Function ExtractText(Separateur, Ligne: String): TStrings;
Var TmpText : String;
Begin
Result := TStringList.Create;
While Pos(Separateur, Ligne) > 0 Do
Begin
TmpText := Copy(Ligne, 1, Pos(Separateur, Ligne) - 1);
If TmpText <> EmptyStr Then Result.Add(TmpText);
Ligne := Copy(Ligne, Pos(Separateur, Ligne) + 1, High(Integer));
End;
If Ligne <> EmptyStr Then Result.Add(Ligne);
End;
Et son utilisation:
procedure TForm1.Button14Click(Sender: TObject);
Var Rslt : TStrings;
i : Integer;
begin
Label1.Caption := '';
Try
Rslt := ExtractText(' ', 'Coucou on est tous ici pour programmer');
If Rslt.count > 0 Then
For i := 0 To Rslt.Count -1 do
Label1.Caption := Label1.Caption + ' ' + Rslt[I];
Finally
Rslt.Free;
End;
end;
{****************************************************************}
@+
Cirec
<HR color =#008000 SIZE=7>
N'oubliez pas de cliquer sur Réponse Acceptée lorsque la réponse vous convient !