Hello, anyone familiar with ansiString?

Have a problem involving a string "1 1", should read "11".

I want to get rid of the space in the middle. Can't change it to a character string or std::string so that isn't an option. Will i have to compare each character inturn with " "?

Using ansiString.Pos(" ") to get position of the " ", but how should i go about removing the space and replacing it with the next character?

Dani AI

Generated

A few clean options for AnsiString (no std::string needed): use the string class built‑ins instead of shifting characters yourself. For a single space remove the easiest is Delete (it removes N chars starting at a 1‑based index). For removing every space in one pass use StringReplace with the rfReplaceAll flag. Example patterns below clarify usage and common pitfalls.

Simple: remove the first found space

AnsiString s = inputString;
int p = s.Pos(" ");    // Pos returns 0 when not found
if (p > 0) s.Delete(p, 1);

The Delete member removes characters starting at index 1 (not 0) and does nothing if the index is out of range. (docwiki.embarcadero.com)

Remove all spaces at once

#include <System.SysUtils.hpp>   // or SysUtils.hpp on older BCB
s = StringReplace(s, " ", "", TReplaceFlags() << rfReplaceAll);

StringReplace is the idiomatic function for find/replace on AnsiString and is much faster and clearer than a manual per‑character loop. (docwiki.embarcadero.com)

Why the original loop failed (as written by ): the code mixes up indexes and length updates. AnsiString is 1‑based (valid character positions are 1..Length()), Pos returns 0 when not found, and you must not decrement the length inside the inner loop or always write to result[space] instead of the loop index. If you must implement manual shifting, loop from the found position up to <= Length() and copy into result[i] (but prefer Delete or StringReplace — they already handle bounds and multibyte issues). (docwiki.embarcadero.com)

Note: 's std::string idea is valid where permitted (it has convenient find/erase), and is right that SubString can be used to rebuild the string — but Delete/StringReplace are simpler and safer for AnsiString code.

Recommended Answers

All 3 Replies

OK here is what I've got at the minute:

String fixString(inputString)
{
    result = inputString;
    int string_size = result.Length();
    while(result.Pos(" "))
    {
        int space = result.Pos(" ");
        for (int i = space; i < string_size; i++)
        {
             result[space] = result[space+1];
             string_size--;
        }
    }
    result.SetLength(string_size);
    return result;
}

Isn't std::string the ANSI string?

Why are you not using std::string? If you were you could just do...

result.erase(result.find(' '));

To remove one space...

-Fredric

ansiString is Borland's C++ builder string class. you can use its SubString method to remove the space. I don't use that compiler, but googled a bit and found this in about 5 seconds.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.