Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Text operations problem :/

Status
Not open for further replies.

Kizpers

Programmer
Mar 18, 2005
10
0
0
PL
Hello,

I'm middle advanced programmer of delphi & I'm using delphi 5. Ok my problem is: I'm looking for a simple and little procedure which should replace some words in TStrings varible. 4example if i have simple text:

" blabalallal test test [something] wee rf
ehjfgjfj [something] oejffj "


It should replece all [something] of an apropriate values...

I tried:
Code:
i := memo1.lines.count;
where : = 0;
repeat
begin
If memo1.lines[where] = '[something]' then
Memo1.lines[where] := 'An apropriate value';
end;
until where = i;
You see that [something] must be in new each line and it couldn't be join with another text.

I see complicated procedure. Help!

Give me some advice!
 
As a general rule, if you want to do something useful with strings then there's already a library function to do it for you. In this case it's StringReplace, simply run that on each line. It wouldn't be too hard to put together a routine you could call to do it for any TStrings instance.
 
Actually, instead of running it on each line of the TStrings, you can run StringReplace on the CommaText property and do all of the TStrings in one shot.

-Dell

A computer only does what you actually told it to do - not what you thought you told it to do.
 
Actually, that's a good point, or you could do it on the Text property.
 
Second thoughts, and sorry for double posting, but DelimitedText would probably be a good idea. Set the delimiter to something that's guaranteed not to be in the text.
 
KempCGDR said:
As a general rule, if you want to do something useful with strings then there's already a library function to do it for you. In this case it's StringReplace, simply run that on each line. It wouldn't be too hard to put together a routine you could call to do it for any TStrings instance.

Not always...
if you want to get item number 6 in a comma separated string, or if you want to delete item 3 in a tab separated string or you want to move item3 to item 8 in a semicolon separated string you have to write it by yourself...

KungTure-RX.jpg

//Nordlund
 
Maybe not a single function for them, but they're not much harder.

Getting can be done with a loop using IsDelimiter and then using Copy to get it out once you're at the right one. Deleting can be a similar loop but using Delete. Moving can be a get and a delete followed by using a combo of the loop plus Insert. You could write these as functions and there you have it, your very own delimited text handlers.
 
This'll need some work but how about this: (not quite sure wether or not this is in Delphi 5 but give it a go)

uses ..., StrUtils;

procedure TForm1.ReplaceSomething(Something,MyText,NewText: string);
begin
while Pos(Something,MyText) > 0 do
AnsiReplaceStr(MyText,Something,NewText);
end;

[bobafett] BobbaFet [bobafett]

Everyone has a right to my opinion.
Great Delphi Websites faq102-5352
 
forgot ... there should be something thatll hold the new text infront of the ansireplacestr, as it is a function that returns strings, my bad.

[bobafett] BobbaFet [bobafett]

Everyone has a right to my opinion.
Great Delphi Websites faq102-5352
 
maybe:

Code:
Function TForm1.ReplaceSomething(Something,MyText,NewText: string): string;
begin
while Pos(Something,MyText) > 0 do
         Result := AnsiReplaceStr(MyText,Something,NewText);
end;

Leslie

Anything worth doing is a lot more difficult than it's worth - Unknown Induhvidual
 
For example :)

[bobafett] BobbaFet [bobafett]

Everyone has a right to my opinion.
Great Delphi Websites faq102-5352
 
Maybe consulting the FAQ area can inspire to some ideas.

How to operate with Strings
faq102-3583

Regards

Steven
 
Thanks for your advices

I'll try it out in few days If I have a time :)

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top