Quick start manual

5-16
Delphi Language Guide
String types
The StrUpper function illustrates the use of pointer indexing to iterate through a null-
terminated string:
function StrUpper(Dest, Source: PChar; MaxLen: Integer): PChar;
var
I: Integer;
begin
I := 0;
while (I < MaxLen) and (Source[I] <> #0) do
begin
Dest[I] := UpCase(Source[I]);
Inc(I);
end;
Dest[I] := #0;
Result := Dest;
end;
Mixing Delphi strings and null-terminated strings
You can mix long strings (AnsiString values) and null-terminated strings (PChar
values) in expressions and assignments, and you can pass PChar values to functions
or procedures that take long-string parameters. The assignment S := P, where S is a
string variable and P is a PChar expression, copies a null-terminated string into a long
string.
In a binary operation, if one operand is a long string and the other a PChar, the PChar
operand is converted to a long string.
You can cast a PChar value as a long string. This is useful when you want to perform
a string operation on two PChar values. For example,
S := string(P1) + string(P2);
You can also cast a long string as a null-terminated string. The following rules apply.
•If S is a long-string expression, PChar(S) casts S as a null-terminated string; it
returns a pointer to the first character in S.
On Windows:
For example, if Str1 and Str2 are long strings, you could call the Win32 API
MessageBox function like this:
MessageBox(0, PChar(Str1), PChar(Str2), MB_OK);
(The declaration of MessageBox is located in the Windows unit.)
On Linux:
For example, if Str is a long string, you could call the opendir system function
like this:
opendir(PChar(Str));
(The declaration of opendir is located in the Libc unit.)
•You can also use Pointer(S) to cast a long string to an untyped pointer. But if S is
empty, the typecast returns nil.