Recent

Author Topic: [SOLVED] Better way to replace text in a stringlist?  (Read 15330 times)

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
[SOLVED] Better way to replace text in a stringlist?
« on: October 04, 2011, 12:05:16 pm »
Hi all,

To replace some text all over a stringlist, I'm using this function:
Code: [Select]
procedure StringListReplace(var SearchIn: TStringList; SearchFor: string;
  ReplaceWith: string);
{description Searches for all occurrences of SearchFor in SearchIn
and replaces them with ReplaceWith}
var
  Counter: integer;
begin
  for Counter := 0 to SearchIn.Count - 1 do
  begin
    SearchIn[Counter] := StringReplace(SearchIn[Counter], SearchFor,
      ReplaceWith, [rfReplaceAll]);
  end; //for
end;


I have a nagging feeling that this is a task that a lot of people would already have done.
Is there perhaps an easier/quicker/cleaner way of doint this?

Thanks.
« Last Edit: October 04, 2011, 01:34:56 pm by BigChimp »
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

fabienwang

  • Sr. Member
  • ****
  • Posts: 449
  • Lazarus is the best
    • My blog
Re: Better way to replace text in a stringlist?
« Reply #1 on: October 04, 2011, 01:22:58 pm »
Yes there is a much easier way:

SearchIn.Text := StringReplace(SearchIn.Text, SearchFor, ReplaceWith, [rfReplaceAll]);

Your procedure is not really useful now :)
I'm using Arch Linux.
Known for: CPickSniff, OpenGrabby
Contributed to: LazPaint

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: Better way to replace text in a stringlist?
« Reply #2 on: October 04, 2011, 01:34:39 pm »
Your procedure is not really useful now :)
Thanks, Fabien, not sad at all to see that procedure go!

I knew about the property but just was a bit dumb at usual. Glad you helped me!
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

 

TinyPortal © 2005-2018