Autor Beitrag
TimKellner
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 73

Win XP Pro
Delphi 7 Enterprise, RAD Studio
BeitragVerfasst: Mo 23.10.06 18:49 
Hallo liebe Community,

so ich schreibe mal meinen ersten Post. Und zwar habe ich ein Problem. Ich habe einen String der wie folgt aussieht.
22 820 "Nam" 0

Wie kann ich davon die einzelnen bereiche wie 22, 820, "Nam" und 0 auslesen?

Ich hoffe mal das dies das richtige Forum ist.


Mit freundlichen Grüßen
Tim
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10185
Erhaltene Danke: 1261

W11x64
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: Mo 23.10.06 18:52 
Moin und :welcome: im Forum!

Das sieht mir nach dem CommaText-Format aus. ;)

Vorschlag:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
var
  SL: TStringList;
  i: Integer;

begin
  SL := TStringList.Create;
  try
    SL.CommaText := '22 820 "Nam" 0';
    for i := 0 to SL.Count-1 do
      ShowMessage(SL.Strings[i]);
  finally
    SL.Free;
  end;

cu
Narses

_________________
There are 10 types of people - those who understand binary and those who don´t.
Sebastian R.
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 72
Erhaltene Danke: 1



BeitragVerfasst: Mo 23.10.06 19:02 
Also ich habe das Problem mit folgendem Quelltext gelöst.
ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
type
  TStringDynArray = array of string;

function Explode(const Separator, S: string; Limit: Integer = 0): TStringDynArray;
var
  SepLen: Integer;
  F, P: PChar;
  ALen, Index: Integer;
begin
  SetLength(Result, 0);
  if (S = '') or (Limit < 0) then Exit;
  if Separator = '' then
  begin
    SetLength(Result, 1);
    Result[0] := S;
    Exit;
  end;
  SepLen := Length(Separator);
  ALen := Limit;
  SetLength(Result, ALen);

  Index := 0;
  P := PChar(S);
  while P^ <> #0 do
  begin
    F := P;
    P := StrPos(P, PChar(Separator));
    if (P = nil) or ((Limit > 0) and (Index = Limit - 1)) then P := StrEnd(F);
    if Index >= ALen then
    begin
      Inc(ALen, 5); // mehrere auf einmal um schneller arbeiten zu können
      SetLength(Result, ALen);
    end;
    SetString(Result[Index], F, P - F);
    Inc(Index);
    if P^ <> #0 then Inc(P, SepLen);
  end;
  if Index < ALen then SetLength(Result, Index); // wirkliche Länge festlegen
end;


Du kannst damit wie folgt deinen String unterteilen.
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
procedure Test;
var
  exploded: TStringDynArray;
begin
  exploded := explode(' ', '22 820 "Nam" 0 ');
  ShowMessage(exploded[0]); // zeigt '22' an
end;


Viel Spass, Sebastian.
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10185
Erhaltene Danke: 1261

W11x64
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: Mo 23.10.06 19:10 
Moin!

Die beiden Ansätze sind allerdings nicht funktional gleich! :mahn: ;)

Wenn user profile iconTimKellner die quoted-elements mit den Quots haben will (also "Nam" mit den Gänsefüßchen), dann wäre dein Vorschlag anzuwenden (ähm, hätte es Suche in: Delphi-Forum, Delphi-Library EXPLODE nicht auch getan, so nebenbei ;)).

Wenn allerdings die quoted-elements "ausgepackt" werden sollen (also "Nam" ohne die Gänsefüßchen), dann wäre mein Vorschlag der richtige.

cu
Narses

_________________
There are 10 types of people - those who understand binary and those who don´t.
Sebastian R.
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 72
Erhaltene Danke: 1



BeitragVerfasst: Mo 23.10.06 19:18 
user profile iconNarses hat folgendes geschrieben:
Wenn user profile iconTimKellner die quoted-elements mit den Quots haben will (also "Nam" mit den Gänsefüßchen), dann wäre dein Vorschlag anzuwenden (ähm, hätte es Suche in: Delphi-Forum, Delphi-Library EXPLODE nicht auch getan, so nebenbei ;)).


So hatte ich es verstanden, denn es war nirgends erwähnt, dass die " entfernt werden sollen. ;) Naja, egal, beide Funktionen haben ihre Vor- und Nachteile... Und ja, Suche in: Delphi-Forum EXPLODE hätte es auch getan, nur ich wollte ihm die Sucherei ersparen. ;)
TimKellner Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 73

Win XP Pro
Delphi 7 Enterprise, RAD Studio
BeitragVerfasst: Mo 23.10.06 21:18 
Danke Leute. Hätte nicht gedacht, dass es so schnell geht. Ich denke mal das ich es morgen ausprobieren. Ich melde mich dann mal ob es geklappt hat
TimKellner Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 73

Win XP Pro
Delphi 7 Enterprise, RAD Studio
BeitragVerfasst: Mi 25.10.06 16:09 
Danke. Es hat geklappt.

*FREU*FREU*