Quick start manual
5-12
Delphi Language Guide
String types
Be careful indexing strings in this way, since overwriting the end of a string can cause
access violations. Also, avoid passing long-string indexes as var parameters, because
this results in inefficient code.
You can assign the value of a string constant—or any other expression that returns a
string—to a variable. The length of the string changes dynamically when the
assignment is made. Examples:
MyString := 'Hello world!';
MyString := 'Hello ' + 'world';
MyString := MyString + '!';
MyString := ' '; { space }
MyString := ''; { empty string }
For more information, see “Character strings” on page 4-5 and “String operators” on
page 4-9.
Short strings
A ShortString is 0 to 255 characters long. While the length of a ShortString can change
dynamically, its memory is a statically allocated 256 bytes; the first byte stores the
length of the string, and the remaining 255 bytes are available for characters. If S is a
ShortString variable, Ord(S[0]), like Length(S), returns the length of S; assigning a
value to S[0], like calling SetLength, changes the length of S. ShortString is maintained
for backward compatibility only.
The Delphi language supports short-string types—in effect, subtypes of ShortString—
whose maximum length is anywhere from 0 to 255 characters. These are denoted by a
bracketed numeral appended to the reserved word string. For example,
var MyString: string[100];
creates a variable called MyString whose maximum length is 100 characters. This is
equivalent to the declarations
type CString = string[100];
var MyString: CString;
Variables declared in this way allocate only as much memory as the type requires—
that is, the specified maximum length plus one byte. In our example, MyString uses
101 bytes, as compared to 256 bytes for a variable of the predefined ShortString type.
When you assign a value to a short-string variable, the string is truncated if it exceeds
the maximum length for the type.
The standard functions High and Low operate on short-string type identifiers and
variables. High returns the maximum length of the short-string type, while Low
returns zero.