BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Find and replace with backslash

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (VCL Components Usage)
View previous topic :: View next topic  
Author Message
freddy mas
Guest





PostPosted: Thu May 17, 2007 8:11 am    Post subject: Find and replace with backslash Reply with quote



Hi,

I have a RichEdit file in which I place markers <MARK> programatically.

Later on, I need to find and replace these <MARK> by "\XH"

I am using the code below but if I use RichEdit3->SelText="\XH" the result will be replacing my mark by XH and the back slash is gone.
If I use RichEdit3->SelText="\\XH" the result will be \\XH double back slash.
I need to replace the mark by SINGLE backslash with XH like this \XH

How can I fix the code to do so?
Thanks

Freddy Mas



int ToEndALL;
int FoundAtALL;
String KeywordALL;



ToEndALL = RichEdit3->Text.Length();
FoundAtALL = RichEdit3->FindText("<MARK>", 0, ToEndALL, TSearchTypes()<< stMatchCase);
if (FoundAtALL != -1)
{
RichEdit3->SelStart = FoundAtALL;
KeywordALL="<MARK>";
RichEdit3->SelLength=KeywordALL.Length();
RichEdit3->SelText="\\XH";
}
Back to top
Bruce Larrabee
Guest





PostPosted: Sat May 19, 2007 7:40 pm    Post subject: Re: Find and replace with backslash Reply with quote



Hi freddy,

I'd suggest using the 'Lines' property of TRichEdit.

'Lines' is a pointer to TStrings. You can then use
the 'Text' property of TStrings. 'Text' will contain
text strings separated by carriage return, line feed
pairs. Just loop through the data copying the source
data to a destination data buffer. When ever you
encounter an instance of the '<MARK>' string replace
it with '\XH'.

Or you could use the 'Strings' property of TStrings.
'Strings' is an array of AnsiStrings you could read
these with something like:

for( int i = 0; i < RichEdit3->Lines->GetCount; i++)
{
strbuf = RichEdit3->Lines->Strings[i].c_str();

// Put some code here for locating '<MARK>'
// in 'strbuf' and replacing it with '\XH'.
// If you find '<MARK>' in the string replace it with
// '\XH' and then do this:

RichEdit3->Lines->Delete(i);

AnsiString SBUF = strbuf;

RichEdit3->Lines->Insert( i, SBUF);
}

(BTW, there's probably a better way if you
can get Remy's attention, but these methods will
work... B)

HTH,

Bruce
Back to top
Bruce Larrabee
Guest





PostPosted: Sat May 19, 2007 7:44 pm    Post subject: Re: Find and replace with backslash Reply with quote



Hi freddy,

Ok, sorry about this I have a correction of
the code I presented:

It would be best to replace:

strbuf = RichEdit3->Lines->Strings[i].c_str();

With something like:

strcpy( strbuf, RichEdit3->Lines->Strings[i].c_str());

Sorry about that,

Bruce
Back to top
Brian Plotkin
Guest





PostPosted: Mon May 21, 2007 3:18 pm    Post subject: Re: Find and replace with backslash Reply with quote

Hello freddy,

An extremely simple way is to do this:

AnsiString newText=StringReplace(RichEdit3->Lines->Text,"<MARK>","\XH",TReplaceFlags()<<rfReplaceAll);

or, if you want the result set to still be contained in separate lines:

TStringList *newLines=new TStringList;
newLines->Text=StringReplace(RichEdit3->Lines->Text,"<MARK>","\XH",TReplaceFlags()<<rfReplaceAll);

Brian

Quote:
Hi,

I have a RichEdit file in which I place markers <MARK
programatically.

Later on, I need to find and replace these <MARK> by "\XH"

I am using the code below but if I use RichEdit3->SelText="\XH" the
result will be replacing my mark by XH and the back slash is gone.

If I use RichEdit3->SelText="\\XH" the result will be \\XH double back
slash.

I need to replace the mark by SINGLE backslash with XH like this \XH

How can I fix the code to do so?
Thanks
Freddy Mas

int ToEndALL;
int FoundAtALL;
String KeywordALL;
ToEndALL = RichEdit3->Text.Length();
FoundAtALL = RichEdit3->FindText("<MARK>", 0, ToEndALL,
TSearchTypes()<< stMatchCase);
if (FoundAtALL != -1)
{
RichEdit3->SelStart = FoundAtALL;
KeywordALL="<MARK>";
RichEdit3->SelLength=KeywordALL.Length();
RichEdit3->SelText="\\XH";
}
Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (VCL Components Usage) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.