B2.1.2 · Programming fundamentals
String manipulation
By the end you can
Extract substrings, concatenate, calculate length, search, and transform strings using 0-based indexing.
In computer science, a is an ordered sequence of characters stored contiguously or as an array of characters. Almost all modern software processes strings: from usernames and URLs to database queries and formatted reports.
Zero-based indexing and substring extraction#
Modern programming languages (including Python and Java) use 0-based indexing. The first character in a string is at index 0, the second is at index 1, and the final character of a string of length $n$ is at index $n - 1$.
A is a contiguous subset of characters extracted from within a longer string. When specifying a range [start, end), standard conventions include the character at index start and exclude the character at index end. Therefore, the number of characters extracted is always equal to end - start.
Substring / Slice Boundaries
IB Pseudocode & Code Equivalence
Python: text[0:8] → "Computer"
Java: text.substring(0, 8) → "Computer"
IB Pseudocode: subString(text, 0, 8) → "Computer"
Common string operations#
The IB syllabus expects students to construct and trace algorithms using key string manipulation functions:
- Length (
len(s)/s.length()): Returns the total count of characters in the string, including whitespace and punctuation. - Concatenation (
+): Joins two or more strings end-to-end to form a single combined string. - Searching (
s.indexOf(sub)/s.find(sub)): Returns the index of the first occurrence of a substring, or-1if not found. - Case transformation (
s.toUpperCase()/s.lower()): Returns a new string with all characters converted to uppercase or lowercase, essential for case-insensitive comparisons. - Splitting (
s.split(delimiter)): Divides a string into an array of substrings separated by a chosen delimiter (such as a comma in CSV data).
Check yourself
String manipulation
Quick questions, made fresh every time and marked here. Do a few until they feel easy.
Exam practice
Paper 1-style questions
3 questions · 8 marks
Write your answer as you would in the exam, then show the mark points and tick each one your answer made. These are our own questions in the style of the exam, not past-paper questions.
1.Given the string s = 'COMPUTING', state the character returned by s[0] and the substring returned by s.substring(3, 7).
[2]State: Give a short answer, no explanation needed.
2.In many programming languages such as Java and Python, strings are immutable. Explain what this means when modifying a string.
[2]Explain: Give reasons: say what happens AND why, or how one thing causes another.
3.An email address is stored in variable email = 'user@school.org'. Construct an algorithm in pseudocode or code to extract just the domain part ('school.org').
[4]Construct: Draw or write it out (a table, a diagram, a program).
In a nutshell
- Strings are ordered sequences of characters accessed via 0-based indexing.
- Substring operations extract contiguous subsets of characters using [start, end) ranges where the end index is exclusive.
- Essential operations include calculating length, concatenation (+), searching (indexOf), case transformation and splitting by delimiter.
- In many modern languages strings are immutable, meaning modifications allocate new string instances in memory.
