Autor Beitrag
Biarchiv
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 688



BeitragVerfasst: Mi 22.04.09 15:48 
Hallo,

folgender Code liefert immer andere Werte obwohl die Ausgangswerte gleich sind.
Mir ist das Aufgefallen wenn ich das Programm immer neu Starte gibt es immer andere Werte, warum?

Dieser soll aus einem Stringbundle, nur lesbare Zeichen rausfischen.

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
function GetStringOnly(Value: String): String;
var
  i, Cur: Integer;
begin
  SetLength(Result, Length(Value));
  Cur := 1;
  for i := 1 to Length(Value) do
    if Value[i] in ['a'..'z''A'..'Z''0'..'9'then
    begin
      Result[Cur] := Value[i];
      Inc(Cur);
    end;
  SetLength(Result, Cur - 1);
end;



Moderiert von user profile iconNarses: Topic aus Sonstiges (Delphi) verschoben am Mi 22.04.2009 um 16:32
Gausi
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 8554
Erhaltene Danke: 481

Windows 7, Windows 10
D7 PE, Delphi XE3 Prof, Delphi 10.3 CE
BeitragVerfasst: Mi 22.04.09 16:10 
Ich würde vermuten, dass das letzte Setlength dir da was kaputt haut. Probier es mal so:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
function GetStringOnly(Value: String): String;
var
  i, Cur: Integer;
begin
  // Result mit Leerzeichen-String initialisieren
  Result := StringOfChar(' ', length(Value));
  Cur := 1;
  for i := 1 to Length(Value) do
    if Value[i] in ['a'..'z''A'..'Z''0'..'9'then
    begin
      Result[Cur] := Value[i];
      Inc(Cur);
    end;
  Result := trim(Result);
end;


Oder alternativ, wenn die Länge von Value nicht zu groß ist:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
function GetStringOnly(Value: String): String;
var
  i: Integer;
begin
  Result := '';
  for i := 1 to Length(Value) do
    if Value[i] in ['a'..'z''A'..'Z''0'..'9'then
      Result := result + Value[i];
end;

_________________
We are, we were and will not be.
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10185
Erhaltene Danke: 1261

W11x64
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: Mi 22.04.09 16:30 
Moin!

Hab´s mal etwas umgestellt und getestet, bei mir klappt´s so: :nixweiss:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
function GetStringOnly(const Value: String): String;
  var
    i, Cur: Integer;
begin
  SetLength(Result, Length(Value));
  Cur := 0;
  for i := 1 to Length(Value) do
    if Value[i] in ['a'..'z''A'..'Z''0'..'9'then begin
      Inc(Cur);
      Result[Cur] := Value[i];
    end;
  SetLength(Result, Cur);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  Edit2.Text := GetStringOnly(Edit1.Text);
end;
cu
Narses

_________________
There are 10 types of people - those who understand binary and those who don´t.
Biarchiv Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 688



BeitragVerfasst: Mi 22.04.09 16:33 
Hallo,

danke, läuft.