Substring

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.

Verified for the 2027 AP Computer Science Principles examLast updated June 2026

What is Substring?

A substring is exactly what it sounds like, a piece of a bigger string. 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 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 Substring matters in AP Computer Science Principles

Substrings live in Unit 3: Algorithms and Programming, Topic 3.4 (Strings), under learning objective AP Comp Sci P 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.

How Substring connects across the course

String Concatenation (Unit 3)

Concatenation and substring are opposite moves on the same object. Concatenation glues strings 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 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)

Substring extraction is a building block inside bigger algorithms. 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.

Is Substring on the AP Computer Science Principles 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 things to remember about Substring

  • 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.

Frequently asked questions about Substring

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.

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.