Autor Beitrag
Tinky
Hält's aus hier
Beiträge: 14



BeitragVerfasst: Fr 10.02.06 17:39 
Hallo,

ich versuche schon seit ein paar Tagen vergeblich meinem Programm beizubringen, dass wenn er auf ein bestimmtes Zeichen (oder einen bestimmten String) in einem String stößt, es dann einen Zeilenumbruch einfügen soll.

Bsp:

Man hat als Eingangsstring z.B so etwas:

writeln; b; k; w;

nun soll das programm wenn es an das Zeichzen ';' stößt einen Zeilenumbruch machen.

Am Ende sollte es ungefähr so aussehen:

writeln;
b;
k;
w;

Dabei sollte jede neu entstandene Zeile ihren eigenen Index in einem Array bekommen.

Vielen Dank für die Hilfe :)

Grüße Tinky


Zuletzt bearbeitet von Tinky am Sa 11.02.06 12:56, insgesamt 1-mal bearbeitet
Spaceguide
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 552


(D3/D7/D8) Prof.
BeitragVerfasst: Fr 10.02.06 17:58 
Probier's mal hiermit, ergab Suche mit Google nach 5sek:

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:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
{#######################################################}
{      Sample Library Routine                           }
{      Author: Henry Bartlett, December 2001            }
{#######################################################}

unit SplitFns;
interface
uses Classes;

function GetNextToken (Const S: string;
                       Separator: char;
                       var StartPos: integer): String;
{Returns the next token (substring) from string S, starting at index
StartPos and ending 1 character before the next occurrence of
Separator
(or at the end of S, whichever comes first).}

{StartPos returns the starting position for the next token, 1 more
than the position
in S of the end of this token}


procedure Split (const S: String;
                 Separator: Char;
                 MyStringList: TStringList);
{Splits a string containing designated separators into tokens and adds
them to MyStringList
NOTE: MyStringList must be Created before being passed to this
procedure
and Freed after use}


function AddToken (const aToken, S: String;
                   Separator: Char;
                   StringLimit: integer): String;
{Used to join 2 strings with a separator character between them and
can be used in a Join function}

{The StringLimit parameter prevents the length of the Result String
from exceeding a preset maximum}


implementation
Uses Sysutils;
{SysUtils is needed to provide the Exception object}

function GetNextToken (Const S: string;
                       Separator: char;
                       var StartPos: integer): String;
var Index: integer;
begin
  Result := '';

{Step over repeated separators}
  While (S[StartPos] = Separator)
  and (StartPos <= length(S))do
   StartPos := StartPos + 1;

  if StartPos > length(S) then Exit; {Returns empty string}

{Set Index to StartPos}
  Index := StartPos;

{Find the next Separator}
  While (S[Index] <> Separator)
  and (Index <= length(S))do
   Index := Index + 1;

{Copy the token to the Result}
  Result := Copy(S, StartPos, Index - StartPos);

{SetStartPos to next Character after the Separator}
  StartPos := Index + 1;
end;

procedure Split (const S: String;
                 Separator: Char;
                 MyStringList: TStringList);
var Start: integer;
begin
  Start := 1;
  While Start <= Length(S) do
    MyStringList.Add (GetNextToken(S, Separator, Start));
end;

function AddToken (const aToken, S: String;
                   Separator: Char;
                   StringLimit: integer): String;
begin
  if Length(aToken) + Length(S) < StringLimit then
    begin
      {Add a separator unless the Result string is empty}
      if S = '' then
        Result := ''
      else Result := S + Separator;

      {Add the token}
      Result := Result + aToken;
    end
  else
  {if the StringLimit would be exceeded, raise an exception}
    Raise Exception.Create('Cannot add token');
end;

end{ SplitFns}
Grishnak
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 221

Windows XP Home
Delphi 7 PE, Delphi 2005 PE
BeitragVerfasst: Fr 10.02.06 18:27 
btw. sehr aussagekräftiger Thread-Titel!

_________________
Mach' etwas idiotensicher und irgendjemand erfindet einen besseren Idioten!
Tino
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Veteran
Beiträge: 9839
Erhaltene Danke: 45

Windows 8.1
Delphi XE4
BeitragVerfasst: Fr 10.02.06 19:25 
Hallo,

bitte ändere den Titel des Topics, da er wenig über das eigentlich Thema verrät. Hier der entsprechende Absatz aus den Richtlinien:

1.2 Beiträge:
Bitte formuliere den Betreff Deiner Beiträge so, dass andere Mitglieder anhand dieser bereits das eigentliche Thema festmachen können. Beiträge wie etwa "Eine Anfängerfrage" oder "Weiß jemand, wie das geht?" lassen den Leser im Unklaren darüber, was das Thema der Diskussion ist.[...]


Einfach oben bei Deinem ersten Beitrag auf user defined image klicken und den Titel ändern. Dank Dir!

Viele Grüße,
Tino
Tinky Threadstarter
Hält's aus hier
Beiträge: 14



BeitragVerfasst: Sa 11.02.06 14:09 
Danke für die Unit, werde die ausprobieren :)

@Admin Habe den Titel des Themas geändert:)
Tinky Threadstarter
Hält's aus hier
Beiträge: 14



BeitragVerfasst: Sa 11.02.06 14:32 
Hallo,

ich komm irgendwie mit dem Parameter (MyStringList: TStringList) der procedure split; nicht klar,
Kann mir jemand erklären wie ich eine Solche String-Liste anlege und dann die einzelnen Zeilen davon auslesen kann?

Grüße
Tinky
BenBE
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 8721
Erhaltene Danke: 191

Win95, Win98SE, Win2K, WinXP
D1S, D3S, D4S, D5E, D6E, D7E, D9PE, D10E, D12P, DXEP, L0.9\FPC2.0
BeitragVerfasst: Sa 11.02.06 14:39 
Wozu so umständlich???

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
procedure SplitBySemicolon(S: String): TStrings;
begin
    Result := TStringList.Create;
    try
        Result.Text := StringReplace(S, ';'';'#13#10, [rfReplaceAll]);
    except
        FreeAndNil(Result);
    end;
end;

_________________
Anyone who is capable of being elected president should on no account be allowed to do the job.
Ich code EdgeMonkey - In dubio pro Setting.