Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Question, re: AnsiStrings

Status
Not open for further replies.

JaybOt

Programmer
Apr 18, 2001
101
GB
Hi all,

Does anyone know how i could refernce a single character in an AnsiString, such as the one provided by the Edit->Text control.

I have tried variations on a theme, such as ..

char* name
for (i = 1; i <= Edit->Text->Length; i++){
name[\i] = Edit->Text[\i];
}

.. but the problem seems to be that the char* in the edit control are not being referenced correctly! Any ideas?

Perhaps declaring 'name' as AnsiString might help? Does anyone tell me how?

Regards,
JayB0t[afro]

&quot;Always know what you say, but don't always say what you know!&quot;
 
If you want a single character in the AnsiString, try this.

char x;
x = Edit->Text[0];

char *x;
x = new char[10];

for (int i=0; i < 10; i++)
x = Edit->Text;

Chris
 
sorry supernat but I cant make heads or tails of what you intend.

you might try thre suggestion below.

----------------------------

AnsiString::SubString

Returns a specified substring of the AnsiString.

AnsiString __fastcall SubString(int index, int count) const;

Description

SubString returns a new AnsiString that is a substring of this AnsiString. The substring contains count characters beginning at index.

tomcruz.net
 
Oops, I meant

for (int i=0; i < 10; i++)
x = Edit->Text;

NOT
for (int i=0; i < 10; i++)
x = Edit->Text;

Sorry bout that! The first part of my message is showing how to access a single character from an AnsiString. The second shows how to actually use that in real code.

Good luck!
Chris
 
With AnsiString's you can't reference a character directly with a char*. Well, you can but it may become invalid on the next statement.

You can either do Edit->Text[index] where index is 1 based. You could copy the text into a new char array like this.

int limit = Edit->Text.Length();
char *name = new char[limit+1];
strcpy(name, Edit->Text.c_str());
for (int i=0;i<limit;i++)
{
char c = name;
// Do something with c
}
delete[] name;

limit adds 1 because you need an extra char for the NUL character at the end of the string.
 
Hiya

A common problem with AnsiString and I don't really know why it is this way cos I fall foul of it every time..

The AnsiString class provides an overloaded method to reference individual characters just as in normal C.

Declaring

AnsiString S = &quot;Fred&quot;;

then referring to S[1] would give you 'F'... and that is the pitfall... under normal C rules you would expect S[0] to give you 'F', whereas in fact S[0] would give you an &quot;Index out of range&quot; exception...

BAsically ...

With ansistring , individual character referencing starts at 1 NOT 0 !

Bizarre but there you go...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top