Board index » cppbuilder » How to removing space chars in an AnsiString?

How to removing space chars in an AnsiString?


2003-08-27 05:47:38 PM
cppbuilder61
Hi all,
How to removing space chars in an AnsiString?
i search the AnsiString method, bug got nothing, mabye i can use substring()
and trim() to achived the effects, but i think it's not effective, how can i
achive this?
for example,
AnsiString str = "2 3 4 q"
i just want to get "234q"
Best regards
 
 

Re:How to removing space chars in an AnsiString?

And for C strings:
void RemoveSpacesFromCstring(char* S)
{
int j = -1;
for (int i=0; i<=strlen(S); i++)
if (*(S+i) != ' ')
*(S+(++j)) = *(S+i);
else
continue;
}
Rodolfo
 

Re:How to removing space chars in an AnsiString?

"Constructor" < XXXX@XXXXX.COM >wrote in message
Quote
How to removing space chars in an AnsiString?
If you want to remove all spaces, the easiest way is to use StringReplace(),
ie:
AnsiString str = "2 3 4 q";
str = StringReplace(str, " ", "", TReplaceFlags() << rfReplaceAll);
Gambit
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (www.grisoft.com).
Version: 6.0.512 / Virus Database: 309 - Release Date: 8/19/03
 

{smallsort}

Re:How to removing space chars in an AnsiString?

"Rodolfo Frino" < XXXX@XXXXX.COM >wrote in message
Quote
I did that because I don't like to use VCL functions for
simple things like that.
Since your functions take an AnsiString as a parameter, you're tied to the
VCL anyway.
Quote
On the other hand my little functions are portable to C#
If you want cross-compiler compatibility, you could just use an STL
std::string with the std::replace() or std::remove() algorithms.
Gambit
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (www.grisoft.com).
Version: 6.0.512 / Virus Database: 309 - Release Date: 8/19/03
 

Re:How to removing space chars in an AnsiString?

Another posibility is
void RemoveSpaces(String& S)
{
int j = 0;
for (int i=1; i<=S.Length(); i++)
if (S[i] != ' ')
S[++j] = S[i];
else
continue;
S[j+1] = '\0';
}
Rodolfo
 

Re:How to removing space chars in an AnsiString?

"Rodolfo Frino" < XXXX@XXXXX.COM >wrote in message
Quote
The function below has an advantage with respect to VCL
counterpart StringReplace, and that is that RemoveSpaces
can be used in both ANSI C and ANSI C++ with NO
modification while StringReplace is limited to C++ Builder.

void RemoveSpaces(char* S)
{
int j = -1;

for (int i=0; i<=strlen(S); i++)
if (*(S+i) != ' ')
*(S+(++j)) = *(S+i);
else
continue;
}

Rodolfo
Except that it takes a char* instead of AnsiString or std::string. This
works with AnsiString because c_str() returns a non constant pointer.
I'm not sure if it would work with std::string as its c_str() returns a
constant.
Additionally, you don't check S for null here. strlen() doesn't like that.
 

Re:How to removing space chars in an AnsiString?

Quote
Except that it takes a char* instead of AnsiString or std::string. This
works with AnsiString because c_str() returns a non constant pointer.
That's not a problem, as you mentioned, due to the magic of c_str() ;-)
Quote
I'm not sure if it would work with std::string as its c_str() returns a
constant.
I haven't tried that either.
Quote
Additionally, you don't check S for null here. strlen() doesn't like
that.
I forgot to check that. Anyway that's a good point you made :-)
Rodolfo