Board index » cppbuilder » Search and Replace multiple Lines in TStringList

Search and Replace multiple Lines in TStringList

Is there an easy way of doing Search and Replace of multiple Lines in
TStringList?

Marc

 

Re:Search and Replace multiple Lines in TStringList


Hi, Marc!

There's no built in function for that purpose. You'll have to write your
own, unfortunately, though they aren't very hard to write.

------
Mark Cashman, TeamB C++ Builder
http://www.temporaldoorway.com/programming/index.htm
C++ Builder, JBuilder programming information
Home of The C++ Builder Programmer's Webring - Join us!
------

Re:Search and Replace multiple Lines in TStringList


Oh, I should also mention that you could use a non-visible TMemo or
TRichEdit to do a search and replace, and since their text content is
based on TStringList, that might be a good choice for your situation.

------
Mark Cashman, TeamB C++ Builder
http://www.temporaldoorway.com/programming/index.htm
C++ Builder, JBuilder programming information
Home of The C++ Builder Programmer's Webring - Join us!
------

Re:Search and Replace multiple Lines in TStringList


Quote
Sault Custom Programming wrote:
> Is there an easy way of doing Search and Replace of multiple Lines in
> TStringList?

Hummm... An easy way? There probably is... I just don't know of any.
I've compiled an ugly, yet working, example of doing so.
It replaces strings in a TMemo, using a search string from a TEdit.
Here it is:
   String currStr;
   int    i, pos;
   char   tempStr[100], replaceStr[100];
   for(i=0;i<Memo1->Lines->Count;i++) {
      currStr = Memo1->Lines->Strings[i];
      if((pos=currStr.Pos(Edit1->Text))) {
         strcpy(tempStr,currStr.c_str());
         strcpy(replaceStr, &tempStr[pos-1+Edit1->Text.Length()]);
         tempStr[pos-1] = 0;
         strcpy(&tempStr[pos-1], "replace string");
         strcat(tempStr, replaceStr);
         Memo1->Lines->Delete(i);
         Memo1->Lines->Insert(i, tempStr);
      }
   }

Adam.

Re:Search and Replace multiple Lines in TStringList


I can replace words no problem. Thanks for the example thought. I need to
search and replace more then one line at a time. I think I have it almost
working anyways.

Marc
Adam O. Savir <ga...@hotmail.com> wrote in message
news:38B3170C.4D4CB63@hotmail.com...

Quote
> Sault Custom Programming wrote:

> > Is there an easy way of doing Search and Replace of multiple Lines in
> > TStringList?

> Hummm... An easy way? There probably is... I just don't know of any.
> I've compiled an ugly, yet working, example of doing so.
> It replaces strings in a TMemo, using a search string from a TEdit.
> Here it is:
>    String currStr;
>    int    i, pos;
>    char   tempStr[100], replaceStr[100];
>    for(i=0;i<Memo1->Lines->Count;i++) {
>       currStr = Memo1->Lines->Strings[i];
>       if((pos=currStr.Pos(Edit1->Text))) {
>          strcpy(tempStr,currStr.c_str());
>          strcpy(replaceStr, &tempStr[pos-1+Edit1->Text.Length()]);
>          tempStr[pos-1] = 0;
>          strcpy(&tempStr[pos-1], "replace string");
>          strcat(tempStr, replaceStr);
>          Memo1->Lines->Delete(i);
>          Memo1->Lines->Insert(i, tempStr);
>       }
>    }

> Adam.

Other Threads