Quick start manual
5-14
Delphi Language Guide
String types
system supports Unicode (UCS-2). The Linux operating system supports UCS-4, a
superset of UCS-2. Borland’s RAD products support UCS-2 on both platforms.
The Delphi language supports single-byte and multibyte characters and strings
through the Char, PChar, AnsiChar, PAnsiChar, and AnsiString types. Indexing of
multibyte strings is not reliable, since S[i] represents the ith byte (not necessarily the
ith character) in S. However, the standard string-handling functions have multibyte-
enabled counterparts that also implement locale-specific ordering for characters.
(Names of multibyte functions usually start with Ansi-. For example, the multibyte
version of StrPos is AnsiStrPos.) Multibyte character support is operating-system
dependent and based on the current locale.
Delphi supports Unicode characters and strings through the WideChar, PWideChar,
and WideString types.
Working with null-terminated strings
Many programming languages, including C and C++, lack a dedicated string data
type. These languages, and environments that are built with them, rely on null-
terminated strings. A nul-terminated string is a zero-based array of characters that
ends with NUL (#0); since the array has no length indicator, the first NUL character
marks the end of the string. You can use Delphi constructions and special routines in
the SysUtils unit (see Chapter 8, “Standard routines and I/O”) to handle nul-
terminated strings when you need to share data with systems that use them.
For example, the following type declarations could be used to store null-terminated
strings.
type
TIdentifier = array[0..15] of Char;
TFileName = array[0..259] of Char;
TMemoText = array[0..1023] of WideChar;
With extended syntax enabled ({$X+}), you can assign a string constant to a statically
allocated zero-based character array. (Dynamic arrays won’t work for this purpose.)
If you initialize an array constant with a string that is shorter than the declared length
of the array, the remaining characters are set to #0. For more information about
arrays, see “Arrays” on page 5-19.
Using pointers, arrays, and string constants
To manipulate null-terminated strings, it is often necessary to use pointers. (See
“Pointers and pointer types” on page 5-27.) String constants are assignment-
compatible with the PChar and PWideChar types, which represent pointers to null-
terminated arrays of Char and WideChar values. For example,
var P: PChar;
ƒ
P := 'Hello world!';