Autor Beitrag
Clemens L.
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 158

Win Xp SP3
D6 Enterprise, Turbo Delphi Explorer
BeitragVerfasst: So 28.05.06 10:42 
Hallo,

ich habe folgendes Problem :

Ich muss mehrere Wörter, die in einer String-Variable stehen trennen, und in einzelne Variablen schreiben.

Beispiel :

ausblenden Delphi-Quelltext
1:
2:
3:
4:
var 
    woerter : string;
begin
woerter := 'erstens zweitens drittens';


Wie kann ich jetzt die ganzen Wörter trennen, um sie dann irgendwo in eine Variable zu speichern?
mkinzler
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 4106
Erhaltene Danke: 13


Delphi 2010 Pro; Delphi.Prism 2011 pro
BeitragVerfasst: So 28.05.06 10:48 
pos + posex oder StrScan

_________________
Markus Kinzler.
Clemens L. Threadstarter
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 158

Win Xp SP3
D6 Enterprise, Turbo Delphi Explorer
BeitragVerfasst: So 28.05.06 10:50 
user profile iconmkinzler hat folgendes geschrieben:
pos + posex oder StrScan



Das versteh ich nicht ganz ... kannst du das etwas näher erklären?
Fighter#1
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 787

Win XP, Ubuntu 8.04
Turbo Delphi 2006, Delphi 2005 Pe, Delphi 5 Pe, Netbeans 6.1, Eclipse, Microsoft VisualC#, Dev C++, PHP, HTML, CSS
BeitragVerfasst: So 28.05.06 10:56 
Copy kopiert einen bestimmten Teil, z.B. vom ersten Buchstaben (1) bis zum Leerzeichen ( Pos(' ',woerter);
Im nächsten schritt löschst du das was bis Jetzt war, mit Delete,
das packst du in ne Schleife und fertig.

_________________
Wer andere beherrscht ist stark,
wer sich selbst beherrscht ist mächtig. Lao Tse
Clemens L. Threadstarter
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 158

Win Xp SP3
D6 Enterprise, Turbo Delphi Explorer
BeitragVerfasst: So 28.05.06 10:59 
danke,

ich glaub jetzt hab ichs verstanden! :wink:
mkinzler
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 4106
Erhaltene Danke: 13


Delphi 2010 Pro; Delphi.Prism 2011 pro
BeitragVerfasst: So 28.05.06 11:20 
oder so:

ausblenden 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:
var
    woerter,s : string;
    wort: array of string;
    i, po: integer;
    p, p2: PChar;
begin
    woerter := 'erstens zweitens drittens';
    p := Pchar( woerter);
    p2 := StrScan( p, ' ');
    po := 1;
    while p2 <> nil do
    begin
       s := copy( woerter, po, p2-p);
       inc(i);
       setLength( wort, i);
       wort[i-1] := s;
       po := p2-p+2;
       p := p2 + 1;
       Application.messagebox( PCHAR(s), PCHAR(''), 1);
       p2 := StrScan( p, ' ');
    end;
    inc(i);
    setLength( wort, i);
    s := p;
    wort[i-1] := s;
    Application.messagebox( PCHAR(s), PCHAR(''), 1);

_________________
Markus Kinzler.
Clemens L. Threadstarter
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 158

Win Xp SP3
D6 Enterprise, Turbo Delphi Explorer
BeitragVerfasst: So 28.05.06 11:31 
Danke,

ich habe es so versucht :

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
procedure TForm1.Button1Click(Sender: TObject);
var
    i : integer;
    Pos1 : integer;
    Pos2 : integer;
    fertig : array[1..3of string;
    strings : string;
begin
strings := 'lol lolll hallo';
i := 1;
repeat
 Pos1 := 1;

 Pos2 := pos(' ',strings);
fertig[i] := Copy(strings,pos1,pos2);
Delete(strings,pos1, Pos2);
i := i + 1;
until I = 3;
showmessage(fertig[1]);
showmessage(fertig[2]);
showmessage(fertig[3]);
end;


Ich will die drei Wörter aus der Variable Strings in das Array fertig schreiben, aber dieser Code kopiert nur die ersten zwei ... :(

Ich habe das Gefühl, dass ich irgendwas dummes übersehen habe, aber ich komm einfach nicht drauf. :roll:
Lannes
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2352
Erhaltene Danke: 4

Win XP, 95, 3.11, IE6
D3 Prof, D4 Standard, D2005 PE, TurboDelphi, Lazarus, D2010
BeitragVerfasst: So 28.05.06 12:04 
Hallo,

Pos2 := pos(' ',strings);ergibt beim letzten Schleifendurchlauf 0, weil hinter dem letzten Wort kein Leerzeichen folgt. Daraus ergibt sich das mit dem nachfolgenden Copy, unter Verwendung der Variablen Pos2 als Count, ein String der Länge 0 ermittelt wird.

_________________
MfG Lannes
(Nichts ist nicht Nichts) and ('' <> nil ) and (Pointer('') = nil ) and (@('') <> nil )
Clemens L. Threadstarter
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 158

Win Xp SP3
D6 Enterprise, Turbo Delphi Explorer
BeitragVerfasst: So 28.05.06 12:10 
Super!!!


Danke, das ist es! :D