String Manipulation Grade 10

Strings are sequences of characters. Delphi provides built-in functions and procedures to analyse, extract, and modify strings.

What is String Manipulation?

String manipulation is the process of working with text inside a program — searching it, pulling pieces out of it, changing it and measuring it. A string is simply a piece of text: a name, an ID number, an email address or a whole sentence are all strings.

Everyday analogy: Think of a string like a row of bead letters on a piece of string, exactly like a beaded keyring that spells a name. Each bead is one character and it sits in a fixed position. String manipulation is what you do when you slide beads off the end, count how many there are, swap a small to a capital bead, or read which bead is sitting in position 3.

WHY IT MATTERS

Almost every real program handles text. When you log in, the computer compares the string you typed against the stored one. When a website greets you by your first name only, it has extracted that name out of your full name. When a form checks that an email contains an "@", it is searching a string. Learning these functions means you can validate input, format output neatly, and pull useful data out of messy text — skills you will use in every program from here on.

Note: In Delphi, the very first character of a string sits at position 1 (not 0). This is why the reference string below numbers its letters starting at 1.

Reference String

All examples on this page use: sSourceText := 'Andre is cute';

Positions: A=1, n=2, d=3, r=4, e=5, (space)=6, i=7, s=8, (space)=9, c=10, u=11, t=12, e=13

Quick Reference

NameTypeWhat it doesReturns
PosFunctionPosition of a substring in a stringInteger
Index [ ]Character at a specific positionString (1 char)
CopyFunctionExtract part of a stringString
InsertProcedureInsert text into a stringModifies original
DeleteProcedureRemove part of a stringModifies original
LengthFunctionNumber of characters in a stringInteger
UpperCaseFunctionConvert to all capitalsString
LowerCaseFunctionConvert to all lowercaseString
T2Term 2 · Basic String Methods (Length, UpperCase)

Length — String Length

Syntax
IntegerVariable := Length(SourceText);
Delphi
iX := Length(sSourceText);   // = 13 (length of 'Andre is cute')
iX := Length('Hello');       // = 5

Group 4 — Changing Case

Changing case means turning all the letters into capitals (UpperCase) or all into small letters (LowerCase). The most important reason we do this is to compare text fairly. The computer thinks 'YES', 'yes' and 'Yes' are three different strings, so if you force them all to lowercase first, they all match.

Everyday analogy: Imagine a teacher marking a one-word answer. Whether a learner wrote "Cape Town", "CAPE TOWN" or "cape town", it is the same correct answer. Lowercasing both the answer and the memo before comparing is how you make sure capitals do not unfairly mark someone wrong.

Important Rule: UpperCase and LowerCase return a new string — they do not change the original unless you store the result back into a variable.

UpperCase and LowerCase

Delphi
sResult := UpperCase(sSourceText);
// 'Andre is cute' → 'ANDRE IS CUTE'

sResult := LowerCase(sSourceText);
// 'Andre is cute' → 'andre is cute'
T3Term 3 · Full String Methods & First Principles

Group 1 — Searching a String

Searching means asking "is this smaller piece of text in there, and if so, where?" The answer you get back is a position number. This is the first thing you do before you can extract or change part of a string, because you usually need to know where something is first.

Everyday analogy: It is like using Find (Ctrl+F) in a document. You type a word, and the computer jumps to the spot where it appears and tells you where it is. Pos is Delphi's version of that "Find".

Pos — Find Position

Returns the integer position of the first occurrence of a substring. Returns 0 if not found.

Syntax
IntegerVariable := Pos(StrToBeFound, SourceText);
Delphi
iX := Pos('e', sSourceText);    // = 5  (first 'e' in 'Andre')
iX := Pos('is', sSourceText);   // = 7
iX := Pos('xyz', sSourceText);  // = 0  (not found)

Index [ ] — Character at Position

Access a single character using square brackets and a position number.

Syntax
StringVariable := SourceText[CharacterPosition];
Delphi
sNewText := sSourceText[2];   // = 'n'
sNewText := sSourceText[5];   // = 'e'

Group 2 — Extracting Part of a String

Extracting means taking a copy of a piece of the string without changing the original. You decide where to start and how many characters to grab. This is how you pull a first name out of a full name, a year out of a date, or an area code out of a phone number.

Everyday analogy: Think of cutting a slice out of a loaf of bread. The loaf (the original string) stays whole; you just take a slice from a chosen spot of a chosen thickness. Copy takes the slice, and Index [ ] takes a single thin slice — just one character.

Copy — Extract Substring

Extracts a section of text starting at a position for a given number of characters.

Syntax
StringVariable := Copy(SourceText, BeginPosition, Length);
Delphi
sNewText := Copy(sSourceText, 10, 4);  // = 'cute'  (start at 10, take 4)
sNewText := Copy(sSourceText, 1, 5);  // = 'Andre' (start at 1, take 5)

Group 3 — Changing a String

Changing a string means actually modifying it — adding text in, taking text out, or measuring it. Unlike extracting, these Insert and Delete procedures change the original string directly, so afterwards the string is different from before.

Everyday analogy: This is like editing a sentence in your exercise book. Insert squeezes a new word into the middle of the sentence; Delete rubs a few words out. Length is just counting how many letters the sentence now has.

Insert — Add Text

Inserts text into an existing string at a given position. Modifies the string directly.

Syntax
Insert(InsertText, SourceText, Position);
Delphi
Insert('very ', sSourceText, 10);
// sSourceText is now: 'Andre is very cute'

Delete — Remove Text

Removes a section of text from a string. Modifies the string directly.

Syntax
Delete(SourceText, Position, Length);
Delphi
Delete(sSourceText, 9, 5);
// sSourceText is now: 'Andre is' (removed ' cute')

Practical Patterns

Reverse a String

Delphi
var
  sInput, sReverse: string;
  i: Integer;
begin
  sInput   := edtInput.Text;
  sReverse := '';
  for i := Length(sInput) downto 1 do
    sReverse := sReverse + sInput[i];
  lblResult.Caption := sReverse;
end;

Count Occurrences of a Character

Delphi — count vowels
var
  sText: string;
  i, iCount: Integer;
begin
  sText  := LowerCase(edtInput.Text);
  iCount := 0;
  for i := 1 to Length(sText) do
    if sText[i] IN ['a','e','i','o','u'] then
      Inc(iCount);
  lblResult.Caption := 'Vowels: ' + IntToStr(iCount);
end;

Extract First Name from "Surname, Name"

Delphi — using Pos and Copy
var
  sFullName, sName, sSurname: string;
  iCommaPos: Integer;
begin
  sFullName  := edtInput.Text;           // e.g. 'Smith, John'
  iCommaPos  := Pos(',', sFullName);
  sSurname   := Copy(sFullName, 1, iCommaPos - 1);
  sName      := Copy(sFullName, iCommaPos + 2, Length(sFullName));
  lblOut.Caption := 'Name: ' + sName + '  Surname: ' + sSurname;
end;

Worked Example — Reading a South African ID Number

A South African ID number is a 13-digit string that packs several pieces of information into fixed positions — a favourite exam question because it uses Length, Copy and the index [ ] all at once.

PositionsMeaningExample (8801235111088)
1–2Year of birth (YY)88
3–4Month (MM)01
5–6Day (DD)23
7–10Gender sequence (5000+ = male)5111 → Male
11Citizenship (0 = SA citizen, 1 = resident)0
12Historically a race indicator; on modern IDs it is always 8 and carries no meaning8
13Check digit (Luhn algorithm)8
Delphi — extract information from an ID
var
  sID, sDOB, sGender, sStatus : string;
  iSeq : Integer;
begin
  sID := edtID.Text;

  // 1. Validate — an ID must be exactly 13 characters long
  if Length(sID) <> 13 then
  begin
    ShowMessage('An ID number must be exactly 13 digits.');
    Exit;
  end;

  // 2. Date of birth — YY at 1, MM at 3, DD at 5 (rebuild as DD/MM/YY)
  sDOB := Copy(sID, 5, 2) + '/' + Copy(sID, 3, 2) + '/' + Copy(sID, 1, 2);

  // 3. Gender — the 4 digits at positions 7-10; 5000 and up = male
  iSeq := StrToInt(Copy(sID, 7, 4));
  if iSeq >= 5000 then
    sGender := 'Male'
  else
    sGender := 'Female';

  // 4. Citizenship — the single character at position 11
  if sID[11] = '0' then
    sStatus := 'SA citizen'
  else
    sStatus := 'Permanent resident';

  // 5. Show the result
  memOut.Lines.Add('Date of birth: ' + sDOB);
  memOut.Lines.Add('Gender: ' + sGender);
  memOut.Lines.Add('Status: ' + sStatus);
end;
The check digit (Grade 11/12 extension)

The 13th digit is a check digit worked out from the other 12 using the Luhn algorithm. A program can recalculate it and compare — if it does not match, the ID was mistyped. That step adds a loop and some arithmetic on top of the string work shown here.