aımnova
DashboardMy LearningCrash CoursesStudy Plan

Aimnova site navigation

Stay in the loop

Get the latest study resources and updates

New features, study tips and exam insights — straight to your inbox.

IB Diploma

  • IB Past Papers
  • IB Study Notes
  • IB Question Bank
  • IB Mock Exams
  • IB Revision

IB Subjects

  • IB Math AA
  • IB Math AI
  • IB Economics
  • IB Business Management
  • IB Physics
  • IB Biology
  • View all IB subjects→

IB Past Papers

  • IB Math AA HL Past Papers
  • IB Math AA SL Past Papers
  • IB Math AI HL Past Papers
  • IB Math AI SL Past Papers
  • IB Economics HL Past Papers
  • IB Economics SL Past Papers
  • IB ESS Past Papers
  • View all past papers→

Study Resources

  • Study Notes
  • Question Bank
  • Mock Exams
  • Crash Courses
  • Flashcards
  • Revision Guide
  • Exam Skills
  • Command Terms
  • Grade Calculator
  • Exam Timetable 2026

Aimnova

  • Features
  • Pricing
  • For Teachers
  • For Schools
  • For Parents
  • About Us
  • Blog
  • Contact
aımnova

IB study, practice and feedback built around the real exam.

TermsPrivacyCookies·© 2026 Aimnova. All rights reserved.c1e448f

Aimnova is not affiliated with or endorsed by the International Baccalaureate Organization (IB).

NotesComputer Science HLTopic 6.1Substrings
Back to Computer Science HL Topics
6.1.25 min read

Substrings (Computer Science HL)

IB Computer Science · Unit 6

AI-powered feedback

Stop guessing — know where you lost marks

Get instant, examiner-style feedback on every answer. See exactly how to improve and what the markscheme expects.

Try It Free

Contents

  1. 1A string is a sequence of positions
  2. 2Taking a slice
  3. 3Doing something useful with it
  4. 4Exam-style question
The big idea: A string is characters in order, and every one has a position.

A substring is a run of those positions taken out as a string of its own.
Positions start at 0: The first character is at position 0, not 1.

So in "HELLO", H is 0, E is 1, L is 2, L is 3 and O is 4. Nearly every substring error starts here.
text = "COMPUTER"
#       01234567     ← positions

print(text[0])        # C     — one character
print(text[0:3])      # COM   — from 0, stopping BEFORE 3
print(text[3:8])      # PUTER
print(len(text))      # 8     — the number of characters
String text = "COMPUTER";
//             01234567     ← positions

System.out.println(text.charAt(0));        // C     — one character
System.out.println(text.substring(0, 3));  // COM   — from 0, stopping BEFORE 3
System.out.println(text.substring(3, 8));  // PUTER
System.out.println(text.length());         // 8     — the number of characters

Free preview

This is the free notes preview

You're reading the free notes. Aimnova Pro unlocks the full study experience — and you can try it with your first topic free to keep:

  • FlashcardsLock in vocabulary and key terms with spaced repetition.
  • Practice questionsAnswer exam-style questions and get instant AI marking.
  • Mock exams & past-paper vaultSit full mocks and see exactly how examiners award marks.
  • Personalised study planA daily plan built around your exam date and weak areas.
Start Studying Free Full access to Aimnova Pro · cancel anytime
The end position is not included: text[2:5] gives the characters at positions 2, 3 and 4 — not 5.

That sounds awkward until you notice the useful consequence: the length of the slice is simply end minus start. 5 − 2 = 3 characters.

Taking part of it

  • text[a:b] — from a, up to but not including b
  • text[:b] — from the start
  • text[a:] — to the end
  • text[-1] — the last character, counting back

Finding and measuring

  • len(text) — how many characters
  • text.find("x") — the position where x starts, or -1
  • "x" in text — True or False
  • Find before you slice, when you do not know the position

Changing it

  • text.upper() and text.lower()
  • text.replace(a, b) — swap one run for another
  • text.split(",") — break into a list at each comma
  • All of these return a new string; the original is unchanged
Strings do not change in place: text.upper() returns an uppercase copy — it does not modify text.

If you want to keep it, you must assign it: text = text.upper(). Forgetting is a very common bug.

Practice with exam-style questions

Answer exam-style questions and get AI feedback that shows you exactly what examiners want to see in a full-marks response.

Try Practice FreeYour first topic is free to keep • No credit card required
Find the position, then slice: Most real problems do not give you the position. You find it first, then use it.

Splitting an email at the @ means finding the @, then slicing either side of it.
email = "ana.silva@school.org"

at = email.find("@")        # 9
user   = email[:at]          # ana.silva
domain = email[at + 1:]      # school.org   — +1 skips the @ itself

print(user, domain)

# Building a student code from a name and a year
name = "Ana Silva"
first, last = name.split(" ")
code = last[:3].upper() + first[0].upper() + "26"
print(code)                  # SILA26
String email = "ana.silva@school.org";

int at = email.indexOf("@");             // 9
String user   = email.substring(0, at);  // ana.silva
String domain = email.substring(at + 1); // school.org   — +1 skips the @ itself

System.out.println(user + " " + domain);

// Building a student code from a name and a year
String name = "Ana Silva";
String[] parts = name.split(" ");
String code = parts[1].substring(0, 3).toUpperCase()
            + parts[0].substring(0, 1).toUpperCase() + "26";
System.out.println(code);                // SILA26
1

Why +1 after find

find returns the position of the @ itself. Slicing from there would include it.

Adding 1 starts one character later, which is what you actually want.

2

Always handle not found

find returns -1 when the text is not there, and -1 used as a position silently gives the wrong answer rather than an error.

Check for -1 before using the result.

How this is tested — you must get the positions right — counting from 0, and excluding the end. It comes up two ways:

Paper 2 — working with code

  • Construct code to extract or build a substring, 3-5 marks
  • State what a given slice produces
  • Explain an off-by-one error

Paper 2 — the algorithmic-thinking question

  • Reason about positions with no code shown
  • Say what a described manipulation would produce
The classic trap: Off by one. Positions start at 0, and the end of a slice is not included. Write the positions above the string on your paper before you answer anything.
IB-style questionConstruct[5 marks]

A product code looks like "BK-2026-1147". Write code to extract the type (BK), the year (2026) and the number (1147), without assuming fixed positions.

Model answer plan

See the answer broken into clear points, with guidance on how each point earns marks.

Claim your free topic

Try an IB Exam Question — Free AI Feedback

Test yourself on Substrings. Write your answer and get instant AI feedback — just like a real IB examiner.

the output of this Java code.

String s = "DATABASE";
int c = 0;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == 'A') {
c = c + 1;
}
}
System.out.println(c);
[2 marks]

Related Computer Science HL Topics

Continue learning with these related topics from the same unit:

6.1.1Variables and data types6.1.3Exception handling6.1.4Debugging6.2.1Static vs dynamic structures
View all Computer Science HL topics
Improve your exam techniqueCommand terms, paper structure, and mark-scheme tips for Computer Science HL
Previous6.1.1Variables and data typesNextException handling6.1.3

29 practice questions on Substrings

Students who practiced this topic on Aimnova scored 82% on average. Try free practice questions and get instant AI feedback.

Try 3 Free QuestionsView All Computer Science HL Topics