Board index » cppbuilder » How to delete anything before: RichEdit1->Lines->Strings[x].Pos("@")

How to delete anything before: RichEdit1->Lines->Strings[x].Pos("@")


2004-05-24 05:59:04 PM
cppbuilder93
hi dear builders,
hope someone can help...i got a TStringList with following lines:
....
XXXX@XXXXX.COM
XXXX@XXXXX.COM
XXXX@XXXXX.COM
....
how can i delete anything BEFORE the "@" i try the following
/***************************************************/
void __fastcall TfrmMainUnit::Button1Click(TObject *Sender)
{
for(int x = 0; x < RichEdit1->Lines->Count; x++)
{
for(int y = 0; y < RichEdit1->Lines->Strings[x].Length(); y++)
{
int pos = RichEdit1->Lines->Strings[x].Pos("@");
RichEdit1->Lines->Strings[x] =
RichEdit1->Lines->Strings[x].Delete(0, pos);
}
}
}
/***************************************************/
but nothing happend...i guess something here is wrong:
RichEdit1->Lines->Strings[x] = RichEdit1->Lines->Strings[x].Delete(0,
pos);
can someone drop me a hint what i should change...?
Oren
 
 

Re:How to delete anything before: RichEdit1->Lines->Strings[x].Pos("@")

"Oren Halvani" < XXXX@XXXXX.COM >wrote in message
Quote
but nothing happend...i guess something here is wrong:

RichEdit1->Lines->Strings[x] = RichEdit1->Lines->Strings[x].Delete(0,
pos);

can someone drop me a hint what i should change...?
While working on AnsiStrings, keep in mind that its index is 1-based. You are
trying to delete from an illegal index (0). Try this instead:
RichEdit1->Lines->Strings[x] =
RichEdit1->Lines->Strings[x].Delete(1, pos);
Also, why are you running through the string for each character? If you are
looking for the last @-character in the string, you could make use of
AnsiString::LastDelimiter();
for(int x = 0; x < RichEdit1->Lines->Count; ++x)
{
AnsiString Line = RichEdit1->Lines->Strings[x];
int pos = Line.LastDelimiter("@");
if(pos != -1)
{
RichEdit1->Lines->Strings[x] = Line.Delete(1, pos);
}
}
Ralph
 

Re:How to delete anything before: RichEdit1->Lines->Strings[x].Pos("@")

Oren Halvani wrote:
Quote
for(int y = 0; y < RichEdit1->Lines->Strings[x].Length(); y++)
Remove the y loop completely. It serves for nothing.
Quote
but nothing happend...i guess something here is wrong:

RichEdit1->Lines->Strings[x] = RichEdit1->Lines->Strings[x].Delete(0,
pos);
Yes. .... Delete(0, pos); is impossible as AnsiString starts at index 1.
....Delete ( 1, pos - 1 );
Hans.
 

{smallsort}

Re:How to delete anything before: RichEdit1->Lines->Strings[x].Pos("@")

thanks to you both :-)
 

Re:How to delete anything before: RichEdit1->Lines->Strings[x].Pos("@")

"Oren Halvani" < XXXX@XXXXX.COM >wrote in message
Quote
for(int x = 0; x < RichEdit1->Lines->Count; x++)
{
for(int y = 0; y < RichEdit1->Lines->Strings[x].Length(); y++)
{
Why are you running two loops? You only need one.
Quote
RichEdit1->Lines->Strings[x] =
RichEdit1->Lines->Strings[x].Delete(0, pos);
AnsiString is 1-indexed, not 0-indexed.
Use this code instead:
void __fastcall TfrmMainUnit::Button1Click(TObject *Sender)
{
RichEdit1->Lines->BeginUpdate();
try
{
for(int x = 0; x < RichEdit1->Lines->Count; ++x)
{
Ansistring temp = RichEdit1->Lines->Strings[x];
int pos = temp.Pos("@");
if( pos>0 )
RichEdit1->Lines->Strings[x] = temp.Delete(1, pos);
}
}
__finally {
RichEdit1->Lines->EndUpdate();
}
}
Gambit