---
title: "Substring — AP CSP Definition & Exam Guide"
description: "A substring is a part of an existing string, pulled out using start and end positions. Learn how AP CSP tests it with indexing, length, and concatenation."
canonical: "https://fiveable.me/ap-comp-sci-p/key-terms/substring"
type: "key-term"
subject: "AP Computer Science Principles"
unit: "Unit 3"
---

# Substring — AP CSP Definition & Exam Guide

## Definition

In AP Computer Science Principles, a substring is a part of an existing string (EK AAP-2.D.2), extracted using a starting and ending position. Substring operations let programs pull out, check, or compare pieces of text, and they show up in string-manipulation questions in Unit 3, Topic 3.4.

## What It Is

A substring is exactly what it sounds like, a piece of a bigger [string](/ap-comp-sci-p/unit-3/data-abstraction/study-guide/kMMTClSiHohfiaHMGFFE "fv-autolink"). The CED keeps it simple in EK AAP-2.D.2: "a substring is part of an existing string." If the string is `"HelloWorld"`, then `"Hello"`, `"World"`, and even `"lloW"` are all substrings, because each one is a continuous chunk of the original.

In code, you grab a substring by giving a starting position and an ending position. In most languages (and in the style AP questions use), `str.substring(0, 5)` on `"HelloWorld"` returns `"Hello"`. Two conventions trip people up here. First, indexing usually starts at 0, so position 0 is the first character. Second, the end [index](/ap-comp-sci-p/key-terms/index "fv-autolink") is typically *exclusive*, meaning the character at the end position is NOT included. That's why `"Computer".substring(2, 5)` gives you `"mpu"` (positions 2, 3, and 4), not `"mput"`. The original string never changes. A substring operation creates a new string and leaves the old one alone.

## Why It Matters

Substrings live in **[Unit 3](/ap-comp-sci-p/unit-3 "fv-autolink"): Algorithms and Programming, Topic 3.4 (Strings)**, under learning objective **[AP Comp Sci P](/ap-comp-sci-p "fv-autolink") 3.4.A**: evaluate expressions that manipulate strings. The CED names exactly two string ideas in the essential knowledge for this objective, concatenation (joining strings) and substrings (extracting part of a string). So when the exam tests string manipulation, there's a good chance you're doing one of these two things, or both in the same expression. Substrings also power real algorithmic tasks, like checking whether a user's input contains a specific word, which means they connect string knowledge to the broader algorithm-building skills Unit 3 is about.

## Connections

### [String Concatenation (Unit 3)](/ap-comp-sci-p/key-terms/string-concatenation)

Concatenation and substring are opposite moves on the same object. Concatenation glues [strings](/ap-comp-sci-p/unit-3/strings/study-guide/0bMtDyDHxqMcwyDgQkjw "fv-autolink") together end-to-end to build a longer string, while a substring carves a piece out of an existing one. AP expressions often chain them, like extracting two substrings and concatenating them into a new word.

### Indexing (String) (Unit 3)

You can't take a substring without indexing, because the start and end positions ARE indexes. Most substring questions are really indexing questions in disguise. If you remember that counting starts at 0 and the end index is excluded, the answer falls out.

### Length (String) (Unit 3)

[Length](/ap-comp-sci-p/unit-3/lists/study-guide/mCE6meIGp5pqs1y5ym3h "fv-autolink") sets the boundaries for valid substring calls. Asking for an end index past the string's length causes an error or unexpected behavior, which is exactly the edge case exam questions like to poke at.

### [Algorithm (Unit 3)](/ap-comp-sci-p/key-terms/algorithm)

Substring extraction is a building block inside bigger [algorithms](/ap-comp-sci-p/key-terms/algorithm "fv-autolink"). A program that searches text for a keyword is really looping through the string, pulling out substrings, and comparing each one to the target. That's string manipulation feeding directly into algorithm design.

## On the AP Exam

Substring questions are multiple-choice territory, and they test you in two ways. First, evaluation: given an expression like `"Computer".substring(2, 5)`, you trace the indexes and pick the resulting string (here, `"mpu"`). The classic traps are forgetting 0-based indexing and forgetting that the end index is exclusive. Second, application: choosing the correct substring-based approach for a task, like checking whether user input contains a specific word. Edge cases matter too. Know what happens when the end index goes past the string's length (you get an error or out-of-bounds problem, not silent success). No released FRQ-style task hinges on the word "substring" itself, but on the Create performance task, extracting pieces of strings is a common way to process input data in your program.

## Substring vs String Concatenation

Both are string operations named in the same CED essential knowledge, but they go in opposite directions. Concatenation (EK AAP-2.D.1) joins two or more strings end-to-end to make a new, longer string. A substring (EK AAP-2.D.2) extracts part of an existing string, producing a shorter one. Quick check: concatenation needs multiple strings as input; substring needs one string plus positions.

## Key Takeaways

- A substring is part of an existing string, extracted using a starting position and an ending position (EK AAP-2.D.2).
- Indexing starts at 0 and the end index is exclusive, so "HelloWorld".substring(0, 5) returns "Hello", the characters at positions 0 through 4.
- Substring operations create a new string and never modify the original.
- Calling substring with an end index larger than the string's length causes an error, a common exam edge case.
- Substring is the extraction half of string manipulation in Topic 3.4; concatenation is the joining half, and AP expressions often combine both.
- Real algorithms use substrings constantly, like checking whether a string contains a specific word by extracting and comparing pieces of it.

## FAQs

### What is a substring in AP Computer Science Principles?

A substring is part of an existing string, defined in the CED as essential knowledge EK AAP-2.D.2 under Topic 3.4 (Strings). You extract it by giving a start position and an end position, like "HelloWorld".substring(0, 5) returning "Hello".

### Does substring include the character at the end index?

No. In the style AP questions use, the end index is exclusive, so the substring stops one character before it. That's why "Computer".substring(2, 5) returns "mpu" (positions 2, 3, and 4), not "mput".

### What's the difference between a substring and string concatenation?

Concatenation joins two or more strings end-to-end to make a longer string (EK AAP-2.D.1), while a substring extracts a piece of one existing string (EK AAP-2.D.2). One builds up, the other carves out.

### Does taking a substring change the original string?

No. A substring operation creates a brand-new string and leaves the original untouched. If you want to keep the result, you have to store it in a [variable](/ap-comp-sci-p/key-terms/variable "fv-autolink").

### What happens if the end index of a substring is bigger than the string's length?

You get an error or out-of-bounds behavior, because you're asking for characters that don't exist. AP questions test this edge case, so always compare your indexes against the string's length first.

## Related Study Guides

- [3.4 Strings](/ap-comp-sci-p/unit-3/strings/study-guide/0bMtDyDHxqMcwyDgQkjw)
- [Big Idea 3: Algorithms and Programming](/ap-comp-sci-p/unit-3/review/study-guide/eOWMqAJUdtnmttaCSlis)

## Structured Data

```json
{"@context":"https://schema.org","@graph":[{"@type":"LearningResource","@id":"https://fiveable.me/ap-comp-sci-p/key-terms/substring#resource","name":"Substring — AP CSP Definition & Exam Guide","url":"https://fiveable.me/ap-comp-sci-p/key-terms/substring","learningResourceType":"Concept explainer","educationalLevel":"AP® / High School","about":{"@id":"https://fiveable.me/ap-comp-sci-p/key-terms/substring#term"},"audience":{"@type":"EducationalAudience","educationalRole":"student"},"dateModified":"2026-06-11T00:50:12.932Z","isPartOf":{"@type":"Collection","name":"AP Computer Science Principles Key Terms","url":"https://fiveable.me/ap-comp-sci-p/key-terms"},"publisher":{"@type":"Organization","name":"Fiveable","url":"https://fiveable.me"}},{"@type":"DefinedTerm","@id":"https://fiveable.me/ap-comp-sci-p/key-terms/substring#term","name":"Substring","description":"In AP Computer Science Principles, a substring is a part of an existing string (EK AAP-2.D.2), extracted using a starting and ending position. Substring operations let programs pull out, check, or compare pieces of text, and they show up in string-manipulation questions in Unit 3, Topic 3.4.","url":"https://fiveable.me/ap-comp-sci-p/key-terms/substring","inDefinedTermSet":{"@type":"DefinedTermSet","name":"AP Computer Science Principles Key Terms","url":"https://fiveable.me/ap-comp-sci-p/key-terms"},"educationalAlignment":[{"@type":"AlignmentObject","alignmentType":"educationalSubject","educationalFramework":"AP® Course and Exam Description","targetName":"AP® Computer Science Principles Unit 3, Topic 3.4, LO 3.4.A"}]},{"@type":"FAQPage","mainEntity":[{"@type":"Question","name":"What is a substring in AP Computer Science Principles?","acceptedAnswer":{"@type":"Answer","text":"A substring is part of an existing string, defined in the CED as essential knowledge EK AAP-2.D.2 under Topic 3.4 (Strings). You extract it by giving a start position and an end position, like \"HelloWorld\".substring(0, 5) returning \"Hello\"."}},{"@type":"Question","name":"Does substring include the character at the end index?","acceptedAnswer":{"@type":"Answer","text":"No. In the style AP questions use, the end index is exclusive, so the substring stops one character before it. That's why \"Computer\".substring(2, 5) returns \"mpu\" (positions 2, 3, and 4), not \"mput\"."}},{"@type":"Question","name":"What's the difference between a substring and string concatenation?","acceptedAnswer":{"@type":"Answer","text":"Concatenation joins two or more strings end-to-end to make a longer string (EK AAP-2.D.1), while a substring extracts a piece of one existing string (EK AAP-2.D.2). One builds up, the other carves out."}},{"@type":"Question","name":"Does taking a substring change the original string?","acceptedAnswer":{"@type":"Answer","text":"No. A substring operation creates a brand-new string and leaves the original untouched. If you want to keep the result, you have to store it in a [variable](/ap-comp-sci-p/key-terms/variable \"fv-autolink\")."}},{"@type":"Question","name":"What happens if the end index of a substring is bigger than the string's length?","acceptedAnswer":{"@type":"Answer","text":"You get an error or out-of-bounds behavior, because you're asking for characters that don't exist. AP questions test this edge case, so always compare your indexes against the string's length first."}}]},{"@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"AP Computer Science Principles","item":"https://fiveable.me/ap-comp-sci-p"},{"@type":"ListItem","position":2,"name":"Key Terms","item":"https://fiveable.me/ap-comp-sci-p/key-terms"},{"@type":"ListItem","position":3,"name":"Unit 3","item":"https://fiveable.me/ap-comp-sci-p/unit-3"},{"@type":"ListItem","position":4,"name":"Substring"}]}]}
```
