Articles   Members Online: 3
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
How to split and concatenate lines of strings in a file Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
19-Oct-02
Category
Object Pascal-Strings
Language
Delphi 2.x
Views
84
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: Jonas Bilinkevicius

I have a disk file with lines of 80 character length. I want to concatenate all 
these lines in one and later split them again into 80 character lines. How can I do 
this?

Answer:

1   function GetOneString(const FilePath: string): string;
2   var
3     List: TStringList;
4     i: Integer;
5   begin
6     Result := '';
7     List := TStringList.Create;
8     try
9       try
10        list.LoadFromFile(FilePath);
11      except
12      end;
13      for i := 0 to List.Count - 1 do
14        Result := Result + List[i];
15    finally
16      list.free;
17    end;
18  end;


To save to a file:

19  procedure SaveStrings(const FilePath: string; const Num: Integer; St: 
20  string);
21  {FilePath: Name of file to save string / Num: Width of strings, 80 in your case / 
22  St: String to save}
23  var
24    f: system.text;
25    i: Integer;
26  begin
27    assignfile(f, FilePath);
28    rewrite(f);
29    i := 0;
30    while ((i + Num) <= Length(st)) do
31    begin
32      writeln(f, copy(st, i + 1, Num));
33      inc(i, Num);
34    end;
35    inc(i);
36    if (i < Length(st)) then
37    begin
38      Writeln(f, copy(st, i, Num));
39    end;
40    closefile(f);
41  end;


A TStringList has some properties like Text and Commatext which return the complete strings separated by CRLF (CarriageReturn and Line Feed) and ',' respectively, therefore you could use any of these to access the strings without wasting extra resources.

			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC