Board index » cppbuilder » AnsiString remove what is in parenthesis

AnsiString remove what is in parenthesis


2007-07-26 05:34:05 AM
cppbuilder69
Hi,
I need to remove anything that is inside parenthesis
in a string. Let's say the string is:
AnsiString S1 = "(red item) red cap (blue item) blue cap (green item) green cap";
It should display in Memo1:
"red cap blue cap green cap"
An example would be great. Thanks.
 
 

Re:AnsiString remove what is in parenthesis

"Harry" < XXXX@XXXXX.COM >wrote in message
Quote
I need to remove anything that is inside parenthesis
in a string.
There is nothing in the VCL to do that for you automatically. You will have
to scan the data manually. For example:
int iLen = S1.Length();
char *pStart = S1.c_str(), *pEnd, *pChr = AnsiStrScan(pStart, '(');
while( pChr )
{
pEnd = AnsiStrScan(CharNext(pChr), ')');
if( !pEnd )
break;
pEnd = CharNext(pEnd);
if( *pEnd == ' ' )
pEnd = CharNext(pEnd);
Move(pEnd, pChr, iLen - (pEnd - pStart));
iLen -= (pEnd - pChr);
pChr = AnsiStrScan(pChr, '(');
}
S1.SetLength(iLen);
Gambit
 

Re:AnsiString remove what is in parenthesis

The straight-forward approach would be to iterate the string looking for
open parenthesis, mark the location, search for close parenthesis, then
delete the content. There are other more optimal solutions but I'm not sure
how optimized you want the code. The code I've shown below will iterate
from the end of the string to the beginning.
// (Not tested)
String str = S1;
int pos = str.Length();
int closeParenPos = 0;
while (pos>0)
{
if (closeParenPos == 0)
{ // Looking for close paren
if (str[pos] == ')')
{
closeParenPos = pos;
}
}
else
{ // Looking for open paren
if (str[pos] == '(')
{
str.Delete(pos, closeParenPos - pos + 1);
closeParenPos = 0;
}
}
--pos;
}
Quote
It should display in Memo1:

"red cap blue cap green cap"
If you remove only what's in the parentheses then the string would be:
" red cap blue cap green cap"
If you want the extra space after the parentheses to be removed then you
will need to alter the algorithm slightly.
- Clayton
 

{smallsort}

Re:AnsiString remove what is in parenthesis

"Harry" < XXXX@XXXXX.COM >wrote:
Quote

I need to remove anything that is inside parenthesis
in a string. Let's say the string is:

AnsiString S1 = "(red item) red cap (blue item) blue cap (green item) green cap";

It should display in Memo1:
Memo1->Lines->Add( RemoveParens(S1) );
//-------------------------------------------------------------
void __fastcall TForm1::RemoveParens( AnsiString &AString )
{
int Pos = AString.Pos( "(" );
while( Pos )
{
AnsiString S = AString.SubString( 1, Pos - 2 );
if( ! S.IsEmpty() ) S += " ";
Pos = AString.Pos( ")" );
AString.Delete( 1, Pos + 1 );
AString = S + AString;
Pos = AString.Pos( "(" );
}
}
//-------------------------------------------------------------
or ... if you do not want the string modified ...
Memo1->Lines->Add( RemoveParens(S1) );
//-------------------------------------------------------------
AnsiString __fastcall TForm1::RemoveParens( AnsiString AString )
{
int Pos = AString.Pos( "(" );
while( Pos )
{
AnsiString S = AString.SubString( 1, Pos - 2 );
if( ! S.IsEmpty() ) S += " ";
Pos = AString.Pos( ")" );
AString.Delete( 1, Pos + 1 );
AString = S + AString;
Pos = AString.Pos( "(" );
}
return AString;
}
//-------------------------------------------------------------
~ JD
 

Re:AnsiString remove what is in parenthesis

Thanks for the examples everyone.
Cool, nice, short and to the point!
Harry