AnsiString is a variable-length string data type.
It can store characters that have a size of one Byte.
In FPC an
AnsiString is implemented as a pointer.
It is a managed data type.
As such it is initialized with
nil as soon as it enters the scope.
Memory for the character sequence is dynamically allocated and freed.
AnsiString points to the first character.
This facilitates interfacing to libraries or foreign functions expecting
For that, an
AnsiString always concludes with a null Byte.
In Pascal, this terminating null Byte has no significance as to the string’s value (including its length).
AnsiString always entails some management data before the first character.
- a code page
- the size of a character
- a reference count
- the length of the string.
|code page||maximum character size||reference count||length||payload||complimentary Null|
|pointer points here ⤴|
Only the length field has significance in Pascal.
In Pascal, an
AnsiString may contain
AnsiString can furthermore be associated with a code page (since 3.0.0).
The data type
AnsiString can be used like any other string data type.
You may assign string literals to an
AnsiString variable as normal.
String values can be compared (
=) just as usual.
The entire pointer-characteristic is transparent.
AnsiString have a 1-based index.
myAnsiString refers to the first character.
length function, and for that matter also
high, will return a string’s length by examining the length data field.
AnsiString is essentially a pointer, copying strings of this type is fast, since only the reference is copied and the reference count increased.
Modifications may trigger a COW.
- The compiler directive
string(without a specified length) to
AnsiStringas a managed data type introduces a certain overhead. See Avoiding implicit try finally section for more explanations.
sizeOfvalue of an
AnsiStringvariable is merely the size of a pointer.
- Assigning an empty string
AnsiStringvariable will in fact assign
nilto the variable and, if the reference count hit zero, release underlying memory (if any was previously allocated at all). Empty strings are not stored as described above.