jexd99
asked on
Advice for processing strings/text?
I am just interested in some advice on this one, as many times I have done something new in Delphi only to find out later I could have done it alot easier! :-)
I am looking for advice on processing a text file. What I plan on doing is reading a text file, parsing it line by line, and char by char, and updating this same text file.
I may be removing lines, adding lines, or removing chars in the line. Because I really haven't done much text file processing like this in Delphi, my initial plans are to open the file, read the the line into an array, parse through it, and rewrite this line back to the file (or maybe just ignore it and get the next line).
I understand this is a brief explanation, but I am assuming that reading each line into an array an parsing through the array is the best way in Delphi. Can someone point me in the right direction on this, and is there some other way which I should research before doing it this way?
Thanks a bunch
I am looking for advice on processing a text file. What I plan on doing is reading a text file, parsing it line by line, and char by char, and updating this same text file.
I may be removing lines, adding lines, or removing chars in the line. Because I really haven't done much text file processing like this in Delphi, my initial plans are to open the file, read the the line into an array, parse through it, and rewrite this line back to the file (or maybe just ignore it and get the next line).
I understand this is a brief explanation, but I am assuming that reading each line into an array an parsing through the array is the best way in Delphi. Can someone point me in the right direction on this, and is there some other way which I should research before doing it this way?
Thanks a bunch
Like this :
var MyText : TMemo;
begin
MyText := TMemo.Create(self);
with MyText do begin
Clear;
Lines.LoadFromFile('HereNa meOfFile') ;
{ Do your stuff }
// e.g.
// delete line 3
Lines.Delete[2]
// add a line
Lines.Add('I add this line');
{ end }
{ save the result to a file }
Lines.SaveToFile('NameOfFi le');
end;
MyText.Free;
MyText := nil;
end;
var MyText : TMemo;
begin
MyText := TMemo.Create(self);
with MyText do begin
Clear;
Lines.LoadFromFile('HereNa
{ Do your stuff }
// e.g.
// delete line 3
Lines.Delete[2]
// add a line
Lines.Add('I add this line');
{ end }
{ save the result to a file }
Lines.SaveToFile('NameOfFi
end;
MyText.Free;
MyText := nil;
end;
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks to both of you. These are a couple of ideas I was looking for. I was considering the delete/rename option, and I'm not real familiar with the memo comonent, but I'll dig into it. It seems like everytime I do something new I find out 5 months later that I could have done it a diff way with 1/2 the code! I just wanted to make sure I wasn't missing something this time around. :-)
Like adding, deleting, ... lines, chars, ... .
You can create a memo at run-time or you place an invisible memo component on the form.
Don't know if this is the best way, just an idea (Normally I would do it like you, but the memo has already some handy functions/procedures)