Belongs to : Type


The AnsiString data type is used to hold sequences of characters, like sentences.

Each character is an AnsiChar, guaranteed to be 8 bits in size.

An AnsiString can hold any number of characters, restricted only by memory.

Unlike ShortStrings, AnsiStrings are pointer referenced variables. Storage is allocated for an AnsiString only when needed. For example, assigning the value of one AnsiString to another does not allocate storage for a copy of the first string. Instead, the reference count of the first string is incremented, and the second AnsiString set to point to it.

But when the second string is changed, new storage is obtained for this new string, and the reference count for the first string is decremented.

When a string is no longer referenced (the last AnsiString referer is set to nil), it is discarded. This is an example of Delphi managing storage on your behalf.

AnsiStrings can be assigned from other strings, from functions that return a string, and with concatenations as in the sample code.

Strings are indexed with 1 for the first character (arrays start with 0 for the first element).

Example code