Pascal strings
last modified November 18, 2026 Free Pascal 3.2.2
A Pascal string is a sequence of characters with a length that is known at run
time. The language has three string families: AnsiString, which
stores bytes in a code page, UnicodeString, which stores UTF-16
code units, and ShortString, which stores up to 255 characters
inline. With the {$H+} directive, the name string
means AnsiString. This article shows how strings are indexed,
sliced, joined, compared, converted, and passed to C-style APIs.
Indexing and length
String indices start at 1, not 0. s[1] is the first character,
s[Length(s)] the last, and an index outside that range is a
runtime error when range checking is enabled. A character can be assigned to a
position, but the assignment cannot change the length of the string.
program indexing;
{$mode objfpc}{$H+}
var
s: string;
i: Integer;
begin
s := 'Pascal';
WriteLn('s = ', s);
WriteLn('Length = ', Length(s));
WriteLn('first = ', s[1]);
WriteLn('last = ', s[Length(s)]);
s[1] := 'p';
WriteLn('changed = ', s);
for i := 1 to Length(s) do
Write(s[i], '.');
WriteLn;
end.
The program changes the first character from P to p
and leaves the rest of the string intact. The loop writes every character
followed by a dot, which makes the one-based indexing visible. Assigning past
the end would not extend the string; use Insert or concatenation
for that.
$ fpc -gl indexing.pas $ ./indexing s = Pascal Length = 6 first = P last = l changed = pascal p.a.s.c.a.l.
Copy, Pos, Delete, and Insert
Four routines cover most substring work. Copy(s, start, count)
returns a new string, Pos(sub, s) returns the first position of a
substring or 0, Delete(s, start, count) removes characters in
place, and Insert(sub, s, position) inserts in place. The
procedures modify their first string argument, so that argument must be a
variable.
program pieces;
{$mode objfpc}{$H+}
var
s, part: string;
begin
s := 'Free Pascal compiler';
WriteLn('Copy(s, 6, 6) = ', Copy(s, 6, 6));
WriteLn('Pos(''Pascal'', s) = ', Pos('Pascal', s));
part := s;
Delete(part, 1, 5);
WriteLn('after Delete = ', part);
part := 'Free Pascal';
Insert(' Object', part, 5);
WriteLn('after Insert = ', part);
end.
Copy(s, 6, 6) starts at the sixth character, which is the
P of Pascal, and takes six characters. Pos
returns 6 for the same reason. Delete(part, 1, 5) removes
Free and leaves Pascal compiler.
Insert(' Object', part, 5) places the new text before the
character that was at position 5, which produces
Free Object Pascal.
$ fpc -gl pieces.pas
$ ./pieces
Copy(s, 6, 6) = Pascal
Pos('Pascal', s) = 6
after Delete = Pascal compiler
after Insert = Free Object Pascal
Concatenation and comparison
The plus operator joins strings, and the comparison operators compare them
character by character. The comparison is case-sensitive and uses the byte
values of the characters, so 'Pascal' and 'pascal'
are different. SameText and CompareText from
SysUtils ignore case, and UpperCase and
LowerCase change the case of a whole string.
program compare;
{$mode objfpc}{$H+}
uses SysUtils;
var
a, b: string;
begin
a := 'Pascal';
b := 'pascal';
WriteLn('a = b: ', a = b);
WriteLn('SameText: ', SameText(a, b));
WriteLn('CompareText: ', CompareText(a, b));
WriteLn('UpperCase: ', UpperCase(a));
WriteLn('LowerCase: ', LowerCase(a));
end.
The first comparison is false because the first letters differ in case.
SameText returns true, and CompareText returns 0,
which means that the strings are equal when case is ignored. A negative result
from CompareText would mean that the first string sorts before the
second, and a positive result the opposite.
$ fpc -gl compare.pas $ ./compare a = b: FALSE SameText: TRUE CompareText: 0 UpperCase: PASCAL LowerCase: pascal
Converting between strings and numbers
SysUtils provides the conversion routines. StrToInt
and StrToFloat parse a whole string and raise
EConvertError when it does not contain a valid number.
TryStrToInt returns a Boolean instead, and
StrToIntDef returns a default value. Val is the
older procedure form: it returns a non-zero error code and leaves the numeric
result in its second argument.
program convert;
{$mode objfpc}{$H+}
uses SysUtils;
var
n: Integer;
code: Integer;
d: Double;
s: string;
begin
n := StrToInt('123');
WriteLn('StrToInt: ', n);
WriteLn('IntToStr: ', IntToStr(456));
d := StrToFloat('3.75');
WriteLn('StrToFloat: ', d:0:2);
WriteLn('FloatToStr: ', FloatToStr(2.5));
s := '42x';
Val(s, n, code);
WriteLn('Val code: ', code);
WriteLn('TryStrToInt: ', TryStrToInt('17', n), ' -> ', n);
WriteLn('StrToIntDef: ', StrToIntDef('oops', -1));
end.
Val('42x', n, code) stores 42 in n and returns 3 in
code, because the letter is not a valid digit. A program that
must reject such input should check the code or use
TryStrToInt. The reading input
article builds interactive parsers from these routines.
$ fpc -gl convert.pas $ ./convert StrToInt: 123 IntToStr: 456 StrToFloat: 3.75 FloatToStr: 2.5 Val code: 3 TryStrToInt: TRUE -> 17 StrToIntDef: -1
PChar and null-terminated strings
Many C libraries and operating-system APIs expect a PChar, a
pointer to a null-terminated sequence of characters. An
AnsiString is already null-terminated internally, so
PChar(s) is a cheap conversion. The pointer remains valid while
the string is alive and unmodified; do not keep it after the string goes out of
scope.
program pcharstr;
{$mode objfpc}{$H+}
uses SysUtils;
var
s: string;
p: PChar;
buffer: array[0..31] of Char;
begin
s := 'Pascal';
p := PChar(s);
WriteLn('StrLen = ', StrLen(p));
WriteLn('first char = ', p^);
Inc(p);
WriteLn('second char = ', p^);
StrPCopy(buffer, 'copied');
WriteLn('buffer = ', StrPas(buffer));
end.
StrLen counts characters until the terminating zero, and
p^ reads the character at the pointer. Incrementing the pointer
moves to the next character, which is why the second line prints
a. StrPCopy copies a Pascal string into a character
buffer and adds the terminator, and StrPas converts such a buffer
back to a Pascal string.
$ fpc -gl pcharstr.pas $ ./pcharstr StrLen = 6 first char = P second char = a buffer = copied
Unicode strings
AnsiString counts bytes, not characters. For UTF-8 text a
character outside ASCII occupies several bytes, so Length returns
the number of bytes and indexing returns one byte. A UnicodeString
stores UTF-16 code units, where a character outside the basic plane occupies
two units. UTF8Decode and UTF8Encode convert between
the two representations.
program unicode;
{$mode objfpc}{$H+}
uses SysUtils;
var
utf8: string;
u: UnicodeString;
begin
utf8 := 'Příliš žluťoučký kůň';
u := UTF8Decode(utf8);
WriteLn('UTF-8 bytes: ', Length(utf8));
WriteLn('UTF-16 units: ', Length(u));
WriteLn('first character: ', u[1]);
WriteLn('back to UTF-8: ', UTF8Encode(u));
end.
The Czech sentence has 20 characters, 29 bytes in UTF-8, and 20 UTF-16 code
units. The two lengths agree here only because every character is in the basic
multilingual plane. u[1] returns a WideChar, which
WriteLn prints correctly when the console encoding supports it.
For text processing that must count characters, convert to
UnicodeString first; characters outside the basic plane still take
two code units, so count surrogate pairs explicitly.
$ fpc -gl unicode.pas $ ./unicode UTF-8 bytes: 29 UTF-16 units: 20 first character: P back to UTF-8: Příliš žluťoučký kůň
Next steps
The string processing article builds splitters, word counters, palindrome checks, and CSV rows from these primitives. The formatted output article shows how strings are padded and aligned in tables, and the reading input article parses strings typed by the user.
Source
In this article we have indexed strings from 1, sliced and modified them with
Copy, Pos, Delete, and
Insert, compared them with and without case, converted between
strings and numbers, passed them as PChar, and handled UTF-8 and
UTF-16 text. The series continues with
Pascal string processing.
Author
List all Pascal tutorials.