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, and check 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 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.

Don't worry if this seems tricky at first; practice makes perfect! Remember, you are just selecting characters from a specific position.

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

This is the most powerful and usually the most complex substring function, as it needs three pieces of information.

Function: MID(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.

Mnemonic: MIDdle needs 3 things: the String, where to Start, and how many characters (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 precisely extract specific parts of a string using indexes and length parameters.


Section 3: Locating Information and Type Conversion

3.1 Finding the Position of a Substring

If you have a long piece of text, you might need to find where a specific character or word begins. This is essential for tasks like checking if an email address has an "@" symbol.

Function: POS() or FIND()

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

  • If the string is not found, it usually returns a value like 0 or -1 (which means "not there").

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

Example 2:
POS("computer", "cat")
The substring "cat" is not in "computer".
Result: 0 (or -1)

3.2 Type Conversion (Casting)

In programming, numbers and strings are stored differently. If you read the number 123 from a form, the computer usually stores it as the string "123". You cannot do mathematical calculations (like multiplication) on strings.

Type Conversion, or Casting, is changing the data type from one form to another.

A) String to Numerical (Integer or Real)

Function: INT() or VAL()

What it does: Converts a string that contains digits into a true numerical value, allowing mathematical operations.

Analogy: This is like changing a receipt (text) into actual money (value) that you can count.

Example:
Let AgeString = "17" (Stored as text)
AgeNumber = INT(AgeString) (Stored as a number)
You can now do AgeNumber + 1, which gives you 18.

B) Numerical to String

Function: STR() or TO_STRING()

What it does: Converts a true numerical value into a string. This is necessary if you need to display the number alongside other text using concatenation.

Example:
Let Score = 95 (Stored as a number)
STR(Score) + "%"
Result: "95%"

Key Takeaway for Section 3

POS() helps locate specific data within text. Type Conversion (INT/STR) is essential for mixing string handling and mathematical processing correctly.


Section 4: Checking Characters (Character Testing)

Sometimes you need to validate user input. For example, if a user enters their age, you need to check that they only used digits, not letters.

These functions usually return a Boolean value (TRUE or FALSE).

4.1 IS_NUMERIC

What it checks: Whether a string contains only digits (0-9).

  • Example: IS_NUMERIC("2024") returns TRUE.
  • Example: IS_NUMERIC("A12") returns FALSE (because of 'A').

4.2 IS_ALPHA (or IS_ALPHABETIC)

What it checks: Whether a string contains only letters (A-Z and a-z).

  • Example: IS_ALPHA("Hello") returns TRUE.
  • Example: IS_ALPHA("Hello!") returns FALSE (because of '!').

4.3 IS_ALPHANUMERIC

What it checks: Whether a string contains only letters and digits (A-Z, a-z, and 0-9). This is often used for usernames or passwords.

  • Example: IS_ALPHANUMERIC("User123") returns TRUE.
  • Example: IS_ALPHANUMERIC("User 123") returns FALSE (because of the space).

4.4 Other Useful Checks (Context Dependent)

  • IS_LOWER: Checks if all characters are lowercase.
  • IS_UPPER: Checks if all characters are uppercase.
👍 Did You Know?

Character testing is a fundamental part of Data Validation. If you see a website reject your password because it contains symbols, it is using character testing functions to check if your input meets their rules!

Key Takeaway for Section 4

Character testing functions allow programs to validate input by checking what types of characters (letters, numbers, both) are present in a string.


Chapter Summary: Essential String Handling Functions

Keep this quick reference guide handy during revision!

Measurement: LEN() (How long is it?)
Joining: + or & (Concatenation)
Extraction: LEFT(), RIGHT(), MID() (Cutting parts of the string)
Searching: POS() or FIND() (Where does this character start?)
Conversion: INT() / STR() (Changing text numbers to math numbers, or vice versa)
Validation: IS_NUMERIC(), IS_ALPHA() (Checking the contents)

You have now mastered the tools needed to manipulate text data in your programs! Great job!