How can I delete numbers and decimals from an ansistring....

Please help -
How can I delete numbers and decimals from an ansistring....
Could you help with how to delete numbers and decimals and then output them into a listbox.
Thanks
how about?

if(string[i] == number)
delete(i, string);
This is the AnsiString:
675.tip 798.webn 23.cqjhrt 37.cxzsk 7865.poy

I will place the above in Edit1 and then with the push of a button I will print it to Edit2 without the decimals and numbers. So the result will only be letters. I tried your above method but did not succeed.
Please for help. Thanks
AnsiString sounds like the Borland C++ Builder version of string.
The current Embarcadero version has transitioned from AnsiString to UnicodeString.

This might be roughly what you are looking for:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void __fastcall TForm1::Button1Click(TObject *Sender)
{
    UnicodeString a = Form1->Edit1->Text;
    UnicodeString b;
    int len = a.Length();
    for (int i=1; i<=len; i++)
    {
        char c = a[i];

        if ((c != '.') && ((c<'0') || (c>'9')))
            b += c;
    }
    Form1->Edit2->Text = b;
}
Thanks so much Chervil. I tested the above on a friends machine... He uses the current Embarcadero. It worked but I do not have embarcadero. I only have c++ borland.
I use c++ borland and hence the reason why ansistrings are better for me.
How can we solve this problem without using [
if ((c != '.') && ((c<'0') || (c>'9')))] but only using length, pos, delete and sub string
Last edited on
Mostly the AnsiString and UnicodeString types work in similar ways, so the above code should work with either type.

Here's another version:
1
2
3
4
5
6
7
8
9
void __fastcall TForm1::Button2Click(TObject *Sender)
{
    AnsiString a = Form1->Edit1->Text;

    while (int n = a.LastDelimiter(".012345679890"))
        a.Delete(n,1);

    Form1->Edit2->Text = a;
}

If for some reason you don't want to use LastDelimiter, you could use a loop to step through the string a character at a time, test the character to see whether it is wanted or not, and delete the unwanted characters.

This page details the functions available for AnsiString:
http://docwiki.embarcadero.com/Libraries/XE2/en/System.AnsiStrings

You may find other documentation is useful too, such as this: http://www.functionx.com/bcb/topics/strings.htm

Edit: I tried a version using the function AnsiPos which was similar to the above, but can search for only one character at a time (at least in this particular problem) and so required an additional loop to test first for '.', then for '0', then for '1' and so on, so the code was not as simple.

http://docwiki.embarcadero.com/Libraries/XE2/en/System.AnsiStrings.AnsiPos
Last edited on
Thanks again Chervil

Is it possible in ansistring to do the following: delete the number,dot and space in that sequence?

For example:

Put this in Edit1: 78.p978y 23.rye 546.b3ft 77.ewxz 14 327.mn9 6kl


Result in Edit2: p978y rye b3ft ewxz 14 mn9 6kl

Is this feasible with Ansistring. Please help me.
Thanks again.
Last edited on
Anything is possible. Regardless of the tools available, the string can be edited according to some set of requirements.

I've added extra spaces here just to clarify my understanding:
78.p978y 23.rye 546.b3ft 77.ewxz 14 327.mn9 6kl
   p978y    rye     b3ft 77.ewxz 14     mn9 6kl

I'm trying to figure out what is the rule which describes the requirements.
It looks like, if the number is followed by a dot, remove both the number and the dot, sometimes. Other times (such as 77.) leave it as it is.

Can you please help in clarifying what it is that you need to do?
Last edited on
Apologies for the typo error....
I meant like this:
Put this in Edit1: 78.p978y 23.rye 546.b3ft 77.ewxz 14 327.mn9 6kl


Result in Edit2: p978y rye b3ft ewxz 14 mn9 6kl

So it should delete numbers, and the dot and the space all the time.
Thanks again.
Thanks for the clarification. Actually, I don't see which space is deleted?

Here's my code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void __fastcall TForm1::Button1Click(TObject *Sender)
{
    AnsiString a = Form1->Edit1->Text;

    while (int n = a.AnsiPos('.'))          // Find '.'
    {
        a.Delete(n,1);                      // Delete the dot
        while (--n > 0)                     // Check preceding character
        {
            if (a[n] >= '0' && a[n] <= '9') // Is it a number?
                a.Delete(n,1);              // Yes, delete it
            else
                break;                      // No, stop
        }
    }

    Form1->Edit2->Text = a;
}

Edit1: 78.p978y 23.rye 546.b3ft 77.ewxz 14 327.mn9 6kl
Edit2: p978y rye b3ft ewxz 14 mn9 6kl


Thanks again. It worked well... You are a star...
Topic archived. No new replies allowed.