Introduction: What is a String and Why Should I Care?

Hello future programmers! Welcome to the section on String Handling. Don’t worry if the name sounds complicated; you use strings every single day!

In Computer Science, a String is simply a sequence of characters. It could be a word ("Hello"), a sentence ("Computer Science is great!"), a password, or a phone number stored as text. Anything that is not a pure mathematical number is usually treated as a string.

This chapter teaches you the essential tools to manipulate, cut, join, convert, and inspect these sequences of characters. Mastering string handling is crucial because almost all programs need to process text input!


Section 1: The Basics – Length and Concatenation

1.1 Understanding String Structure (Indexing)

Imagine a string is like a set of lockers lined up. Each character sits in its own locker, and that locker has a specific address, called the index.

  • The index is the position number of the character within the string.
  • Important Note: Depending on the programming language, indexing often starts at position 0 (zero) or position 1 (one). For the exam, always pay attention to the context, but the principle remains the same.

Example String: "APPLE"

If indexing starts at 1:
P1: A, P2: P, P3: P, P4: L, P5: E

1.2 Finding the Length of a String

The first step in handling a string is knowing how long it is.

Function: LENGTH() or LEN()

What it does: Counts the total number of characters in the string, including spaces, punctuation, and special characters.

Example:
LEN("AQA Exam")
Count the characters: A, Q, A, (space), E, x, a, m.
Result: 8

1.3 Joining Strings Together (Concatenation)

Concatenation is the process of joining two or more strings end-to-end to create one longer string.

Operators used: Usually the plus sign (+) or the ampersand (&).

Analogy: Concatenation is like snapping two train carriages together.

Example:
Let FirstName = "Alex"
Let LastName = "Smith"
FullName = FirstName + " " + LastName
Result: "Alex Smith"

⚠ Common Mistake Alert!

If you concatenate two strings that contain numbers (e.g., "2" and "5"), the result is "25", not the mathematical sum of 7. Concatenation joins them as text!

Key Takeaway for Section 1

We use LEN() to measure length and Concatenation (like + or &) to join strings together.


Section 2: Substring Operations (Cutting and Slicing)

Often, you only need a small part of a long string (like just the year from a date, or the first letter of a name). We use Substring functions to achieve this.

A Substring is a part of a larger string.

2.1 Extracting from the Left

Function: LEFT(String, Number of Characters)

What it does: Takes the specified number of characters starting from the very beginning (the left side) of the string.

Example:
LEFT("Programmer", 4)
We start at 'P' and take 4 characters (P, r, o, g).
Result: "Prog"

2.2 Extracting from the Right

Function: RIGHT(String, Number of Characters)

What it does: Takes the specified number of characters starting from the very end (the right side) of the string.

Example:
RIGHT("Computer Science", 7)
We count back 7 characters: S, c, i, e, n, c, e.
Result: "Science"

2.3 Extracting from the Middle

Function: MID(String, Start Position, Number of Characters) or SUBSTRING(String, Start Position, Number of Characters)

What it does: Extracts a part of the string starting at a specified position and continuing for a specified length.

Example: Let's assume index starts at 1.
String: "DATA2024FILE"

MID("DATA2024FILE", 5, 4)

  1. Start Position (5): The 5th character is '2'.
  2. Number of Characters (4): We start at '2' and take 4 characters (2, 0, 2, 4).

Result: "2024"

🔍 Quick Review: Substring Parameters

LEFT: String, N (how many from the start)
RIGHT: String, N (how many from the end)
MID: String, Start Position, Length (how many from that position)

Key Takeaway for Section 2

Substring functions (LEFT, RIGHT, MID) allow us to extract specific parts of a string using indexes and length parameters.


Section 3: Locating Information and Type Conversions

3.1 Finding the Position of a Substring

If you have a string, you might need to find where a specific character or sequence begins. This is essential for tasks like checking if an email address contains an "@" symbol.

Function: POSITION() or POS() / FIND()

What it does: Returns the starting index (position number) of the first occurrence of a search character or substring within a target string.

Example: Assume index starts at 1.
POS("exam@school.com", "@")
Counting from the start: e(1), x(2), a(3), m(4), @(5).
Result: 5

3.2 String Conversion Operations (Casting)

In programming, numerical values and strings are stored differently. You cannot perform arithmetic on text. OxfordAQA requires you to know four key conversions:

A) String to Integer

Function: STRING_TO_INT() or INT()
What it does: Converts a string containing whole numbers into an integer.
Example: INT("42") evaluates to the integer value 42.

B) String to Real

Function: STRING_TO_REAL() or REAL() / FLOAT()
What it does: Converts a string containing decimal numbers into a real/floating-point number.
Example: REAL("3.14") evaluates to the real value 3.14.

C) Integer to String

Function: INT_TO_STRING() or STR()
What it does: Converts an integer into a string representation so it can be concatenated with text.
Example: STR(95) + "%" evaluates to "95%".

D) Real to String

Function: REAL_TO_STRING() or STR()
What it does: Converts a real (decimal) number into a string.
Example: "Total: $" + STR(19.99) evaluates to "Total: $19.99".


Section 4: Character Code Conversions

Computers store every character as a numerical code using character sets such as ASCII or Unicode. You need to know how to convert between a character and its underlying character code.

4.1 Convert Character to Character Code

Function: ASC(), CHAR_TO_CODE(), or ord()

What it does: Takes a single character and returns its numerical character code (such as its ASCII integer value).

Example:
In ASCII, uppercase 'A' is represented by the number 65.
ASC("A") evaluates to 65.
ASC("a") evaluates to 97.

4.2 Convert Character Code to Character

Function: CHR(), CODE_TO_CHAR(), or chr()

What it does: Takes a numerical character code and returns the corresponding character.

Example:
CHR(65) evaluates to "A".
CHR(66) evaluates to "B".

👍 Character Code Tip

Converting between characters and character codes is especially useful in cryptography (like Caesar ciphers) and comparing alphabetical ordering!


Chapter Summary: Essential String Handling Functions

Keep this quick reference guide handy during revision!

  • Length: LEN(str) / LENGTH(str) — Returns number of characters.
  • Position: POS(str, substr) — Returns the index position of a substring.
  • Substring: LEFT(str, n), RIGHT(str, n), MID(str, start, len) — Extracts pieces of text.
  • Concatenation: + or & — Joins strings end-to-end.
  • Character to Code: ASC(char) / CHAR_TO_CODE(char) — Converts character to its integer code (e.g., 'A' → 65).
  • Code to Character: CHR(code) / CODE_TO_CHAR(code) — Converts character code to character (e.g., 65 → 'A').
  • String Conversions: INT(), REAL(), STR() — Converts between strings, integers, and real numbers.

You have now mastered the required string handling operations for OxfordAQA IGCSE Computer Science!