String Functions
Pascal provides several standard functions for manipulating strings. Length returns the character count, Copy extracts a substring, and Concat joins multiple strings together. Pos searches for the position of a substring, while Delete and Insert modify a string variable in place. Combining these functions covers most string manipulation tasks concisely.
Syntax
{ -----------------------------------------------
Basic usage of string functions
----------------------------------------------- }
{ Length — returns the number of characters in a string }
n := Length(s);
{ Copy — extracts a substring (position is 1-based) }
sub := Copy(s, start_pos, count);
{ Concat — concatenates multiple strings and returns the result }
result := Concat(s1, s2, s3);
{ The + operator produces the same result }
result := s1 + s2 + s3;
{ Pos — returns the position of the first occurrence of a substring (0 if not found) }
idx := Pos(substring, s);
{ Delete — removes characters from the string variable in place (no return value) }
Delete(var s, start_pos, count);
{ Insert — inserts a string into the variable at the specified position (no return value) }
Insert(src_string, var dst, insert_pos);
Function Reference
| Function | Description |
|---|---|
Length(s) | Returns the number of characters (bytes) in string s as an integer. Returns 0 for an empty string. |
Copy(s, i, n) | Extracts and returns n characters from string s starting at position i. Position is 1-based. |
Concat(s1, s2, ...) | Concatenates all argument strings from left to right and returns the result. Produces the same result as the + operator. |
Pos(sub, s) | Returns the position at which substring sub first appears in string s. Returns 0 if not found. |
Delete(var s, i, n) | Deletes n characters from string variable s starting at position i. Has no return value; modifies the variable destructively. |
Insert(src, var dst, i) | Inserts string src into string variable dst at position i. Has no return value; modifies the variable destructively. |
Sample Code
rgg_string_functions.pas
{ rgg_string_functions.pas — sample demonstrating Pascal string functions }
{ Uses Like a Dragon (Yakuza) character data to show }
{ the behavior of Length / Copy / Concat / Pos / Delete / Insert }
{
Compile and run:
fpc rgg_string_functions.pas && ./rgg_string_functions
}
program rgg_string_functions;
var
{ -----------------------------------------------
String variable declarations
----------------------------------------------- }
last_name : String; { last name (romanized) }
first_name : String; { first name (romanized) }
full_name : String; { full name }
title : String; { title/alias }
profile : String; { profile string }
sub_str : String; { result of substring extraction }
idx : Integer; { Pos search result }
begin
{ -----------------------------------------------
Set data for Kazuma Kiryu
----------------------------------------------- }
last_name := 'Kiryu';
first_name := 'Kazuma';
WriteLn('===== Like a Dragon String Functions Sample =====');
{ -----------------------------------------------
Length — check string length
----------------------------------------------- }
WriteLn('--- Length ---');
WriteLn('last_name : ', last_name, ' Length = ', Length(last_name));
WriteLn('first_name : ', first_name, ' Length = ', Length(first_name));
WriteLn('');
{ -----------------------------------------------
Concat — concatenate strings
----------------------------------------------- }
full_name := Concat(last_name, ' ', first_name);
WriteLn('--- Concat ---');
WriteLn('Concat(last_name, '' '', first_name) : ', full_name);
WriteLn('+ operator produces the same result : ', last_name + ' ' + first_name);
WriteLn('');
{ -----------------------------------------------
Copy — extract a substring
----------------------------------------------- }
WriteLn('--- Copy ---');
{ full_name = 'Kiryu Kazuma' (length 12) }
sub_str := Copy(full_name, 1, 5); { first 5 characters = 'Kiryu' }
WriteLn('Copy(full_name, 1, 5) : ', sub_str);
sub_str := Copy(full_name, 7, 6); { 6 characters from position 7 = 'Kazuma' }
WriteLn('Copy(full_name, 7, 6) : ', sub_str);
sub_str := Copy(full_name, 3, 3); { 3 characters from position 3 = 'ryu' }
WriteLn('Copy(full_name, 3, 3) : ', sub_str);
WriteLn('');
{ -----------------------------------------------
Pos — search for a substring's position
----------------------------------------------- }
title := 'Kiryu - The Dragon of Dojima';
WriteLn('--- Pos ---');
WriteLn('title : ', title);
idx := Pos('Dragon', title); { position where 'Dragon' begins }
WriteLn('Pos(''Dragon'', title) : ', idx);
idx := Pos('Dojima', title); { position of 'Dojima' }
WriteLn('Pos(''Dojima'', title) : ', idx);
idx := Pos('Majima', title); { not found: returns 0 }
WriteLn('Pos(''Majima'', title) : ', idx, ' (returns 0 when not found)');
WriteLn('');
{ -----------------------------------------------
Delete — remove part of a string (modifies the variable in place)
----------------------------------------------- }
profile := 'Kazuma Kiryu - 4th Chairman of Tojo Clan';
WriteLn('--- Delete ---');
WriteLn('Before : ', profile);
Delete(profile, 16, 26); { remove '4th Chairman of Tojo Clan' }
WriteLn('After Delete: ', profile);
WriteLn('');
{ -----------------------------------------------
Insert — insert a string at the specified position (modifies in place)
----------------------------------------------- }
WriteLn('--- Insert ---');
WriteLn('Before : ', profile);
Insert('Dragon of Dojima', profile, 16); { insert a new title at the removed position }
WriteLn('After Insert: ', profile);
WriteLn('');
{ -----------------------------------------------
Combining Delete / Insert for Goro Majima
----------------------------------------------- }
WriteLn('--- Goro Majima processing ---');
profile := 'Goro Majima - Mad Dog of Shimano';
WriteLn('Original profile : ', profile);
{ Remove 'Mad Dog of Shimano' and replace with a new title }
idx := Pos('Mad Dog', profile); { find the start position }
Delete(profile, idx, Length('Mad Dog of Shimano'));
Insert('The Majima Construction CEO', profile, idx);
WriteLn('After title change : ', profile);
WriteLn('');
{ -----------------------------------------------
Combining Concat and Length for Masaru Watase
----------------------------------------------- }
WriteLn('--- Concat and Length combined ---');
full_name := Concat('Masaru', ' ', 'Watase');
WriteLn('full_name : ', full_name);
WriteLn('Length : ', Length(full_name));
WriteLn('First 6 chars (Copy) : ', Copy(full_name, 1, 6));
WriteLn('=========================================');
end.
fpc rgg_string_functions.pas && ./rgg_string_functions
Free Pascal Compiler version ...
Linking ./rgg_string_functions
===== Like a Dragon String Functions Sample =====
--- Length ---
last_name : Kiryu Length = 5
first_name : Kazuma Length = 6
--- Concat ---
Concat(last_name, ' ', first_name) : Kiryu Kazuma
+ operator produces the same result : Kiryu Kazuma
--- Copy ---
Copy(full_name, 1, 5) : Kiryu
Copy(full_name, 7, 6) : Kazuma
Copy(full_name, 3, 3) : ryu
--- Pos ---
title : Kiryu - The Dragon of Dojima
Pos('Dragon', title) : 13
Pos('Dojima', title) : 23
Pos('Majima', title) : 0 (returns 0 when not found)
--- Delete ---
Before : Kazuma Kiryu - 4th Chairman of Tojo Clan
After Delete: Kazuma Kiryu -
--- Insert ---
Before : Kazuma Kiryu -
After Insert: Kazuma Kiryu - Dragon of Dojima
--- Goro Majima processing ---
Original profile : Goro Majima - Mad Dog of Shimano
After title change : Goro Majima - The Majima Construction CEO
--- Concat and Length combined ---
full_name : Masaru Watase
Length : 13
First 6 chars (Copy) : Masaru
=========================================
Common Mistakes
Using 0-based indexing
Pascal strings are 1-based. Writing Copy(s, 0, n) or Delete(s, 0, n) produces unintended behavior. To access the first character, use position 1, as in Copy(s, 1, 1).
Copy range exceeds the string length
If the start position i in Copy(s, i, n) exceeds the length of the string, Free Pascal returns an empty string rather than raising an error. When unexpected empty strings are returned, verify that i is within the range of Length(s).
Forgetting that Delete / Insert modify the variable in place
Delete and Insert have no return value; they modify the variable passed as a var argument directly. Attempting to assign their result — for example result := Delete(s, i, n) — causes a compile error. This is different from Copy, Concat, and Pos, which return new values.
Overview
An important point about Pascal's string functions is that positions are 1-based. Copy(s, 1, n) extracts n characters from the beginning; writing Copy(s, 0, n) is accepted by Free Pascal but produces unintended results, so always start from 1. Delete and Insert receive their variable as a var argument and modify it in place after the call. Unlike Copy, Concat, and Pos — which return a new value — the original variable is changed directly, which requires care. Passing the return value of Pos as the position argument to Delete or Insert enables concise replacement at dynamically determined positions. For the basics of Char and String types, see Char / String (character and string types); for conversion to and from character codes, see Ord / Chr (character code conversion).
If you find any errors or copyright issues, please contact us.