100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Programming

Strings in Pascal

Learn how Pascal's length-prefixed strings work, from fixed-length capped strings to dynamic AnsiString, plus core string functions.

Data StructuresBeginner8 min readJul 10, 2026
Analogies

Introduction to Strings

Pascal's traditional 'string' type is a length-prefixed array of characters, historically declared as 'string[40]' to cap it at 40 characters, where byte 0 stores the current length and characters occupy bytes 1 through the declared maximum; modern dialects like Free Pascal's 'AnsiString' relax this to a dynamically growing, reference-counted string with no fixed cap. This length-prefix design means Length(s) is an O(1) operation, unlike C-style null-terminated strings that require scanning to find the end.

🏏

Cricket analogy: A length-prefixed string is like a scorecard that states 'this innings lasted exactly 47 balls' right at the top, so you instantly know the length without counting every ball bowled — unlike a null-terminated string, which is like counting balls until you spot a 'stumps drawn' marker.

Common String Operations

Pascal provides built-in functions for string manipulation: Length(s) returns the character count, Copy(s, start, count) extracts a substring, Pos(sub, s) finds the index of a substring (returning 0 if absent), Concat(s1, s2) or the '+' operator joins strings, and Delete/Insert modify a string in place by removing or inserting characters at a given position. For example, 'Copy('Pascal', 1, 3)' returns 'Pas', and 'Pos('sca', 'Pascal')' returns 3.

🏏

Cricket analogy: Copy(s, start, count) is like extracting just the 'middle overs' (overs 11 to 40) from a full match commentary transcript — you pull a specific slice without needing the whole innings text.

Fixed-Length Strings vs. AnsiString

A declared 'string[10]' truncates silently at 10 characters if you assign a longer value, which is a common source of subtle bugs when porting old code, whereas AnsiString in modern Free Pascal/Delphi grows dynamically and is reference-counted, meaning assignment ('s2 := s1') is a cheap pointer copy with copy-on-write semantics rather than an immediate full duplication. Mixing the two styles in the same program is legal but can silently truncate data when a long AnsiString is assigned into a short fixed string.

🏏

Cricket analogy: It's like a scoreboard display limited to 3 digits that silently shows '999' instead of '1000' for a rare high team total — a fixed 'string[10]' truncates the same way, quietly dropping data past its cap.

pascal
var
  s: string;
  pos1: Integer;
begin
  s := 'Pascal Programming';
  writeln('Length: ', Length(s));
  writeln('First 6 chars: ', Copy(s, 1, 6));
  pos1 := Pos('Prog', s);
  writeln('Position of Prog: ', pos1);
  Delete(s, 1, 7);
  writeln('After delete: ', s);
  Insert('Modern ', s, 1);
  writeln('After insert: ', s);
end.

Assigning a value longer than a fixed-length string's declared capacity (e.g. 'var name: string[5]; name := 'Alexandria';') silently truncates to the first 5 characters without raising an error in many compiler configurations — always size fixed strings generously or use AnsiString when input length is unpredictable.

  • Pascal's 'string' type stores a length prefix, making Length(s) an O(1) operation.
  • Length, Copy, Pos, Concat/'+', Delete, and Insert are the core built-in string operations.
  • Copy(s, start, count) extracts a substring; Pos(sub, s) finds a substring's index or 0.
  • Fixed-length strings like string[10] silently truncate values that exceed their capacity.
  • AnsiString grows dynamically and uses reference-counted, copy-on-write assignment.
  • Mixing fixed strings and AnsiString in one program is legal but risks silent truncation.

Practice what you learned

Was this page helpful?

Topics covered

#Programming#PascalStudyNotes#StringsInPascal#Strings#Pascal#Common#String#StudyNotes#SkillVeris#ExamPrep