01
Overview

Parsing fields with indexOf and substring.

Find the delimiter's position; split the string around it. The pair handles email addresses, names, and structured input.

indexOf returns the position of a target char or substring (or -1 when absent). substring extracts a portion using start and end indices. Together they parse structured strings: "Jane Doe" splits into first and last name; "user@example.com" splits into local part and domain.

On the AP exam, this pair is the most common String manipulation pattern in parsing FRQs.

02
Core Concept

Locate the delimiter; cut on each side.

Two substring calls produce the two fields.

Email parsing

public String getDomain(String email) {
    int atIndex = email.indexOf('@');
    return email.substring(atIndex + 1);
}

public String getLocalPart(String email) {
    int atIndex = email.indexOf('@');
    return email.substring(0, atIndex);
}

For "jane@example.com": atIndex = 4. getDomain returns "example.com"; getLocalPart returns "jane".

substring index rules

  • s.substring(start) — from start to end, inclusive of start.
  • s.substring(start, end) — from start up to but NOT including end.
  • Length of result = end - start.

Guard against missing delimiter

int atIndex = email.indexOf('@');
if (atIndex == -1) return "";   // no @ found

Without the guard, substring(-1 + 1) would return the entire String — silently producing a wrong "domain".

03
Key Vocabulary

Parsing vocabulary.

Used in structured-input problems.

Vocabulary
  • indexOf — returns position or -1 if not found.
  • substring — extracts a portion by index.
  • Delimiter — character separating fields.
  • Local part — left of the @ in an email.
  • Domain — right of the @ in an email.
  • Inclusive start, exclusive end — substring's index convention.
04
AP Exam Focus

How indexOf + substring is tested.

Off-by-one and missing delimiter dominate.

Exam Focus
  • indexOf returns -1 when absent. Guard before substring.
  • substring's end is exclusive. s.substring(0, 3) gets chars at 0, 1, 2.
  • Skip the delimiter itself. Use indexOf + 1 for the right side.
  • Empty substring possible. substring(0, 0) returns "".
05
Worked Example

Parse first and last name.

Split on space.

Worked Example AP-style reasoning

Input: "Jane Doe".

indexOf(' ') = 4.

  • First name: substring(0, 4) → "Jane".
  • Last name: substring(5) → "Doe" (skip the space).

For "JaneDoe" (no space): indexOf returns -1; substring(0, -1) throws an exception. Always guard first.

06
Common Mistakes

Parsing pitfalls.

Index arithmetic is the top issue.

Watch Out
  • Including the delimiter in the right side. Forgetting + 1 includes the '@' or space.
  • No -1 guard. substring on -1 crashes.
  • Confusing substring's inclusive/exclusive. Start inclusive; end exclusive.
  • Off-by-one in the left side. substring(0, atIndex) goes up to but excludes atIndex.
07
Reference Table

Parsing template.

Find, guard, cut.

indexOf + substring Reference

AP Quick Reference
OperationCodeResult
Find delimiterindexOf(d)Position or -1.
Left sidesubstring(0, i)Before delimiter.
Right sidesubstring(i + 1)After delimiter.
No delimiterCheck for -1Return default.
08
Practice Tip

How to master parsing.

Guard first; then cut.

Practice Strategy
  • MCQ Practice — sketch the string with index labels; verify each substring boundary.
  • Java Lab — parse emails, names, and "key=value" pairs. Test missing delimiters.
  • FRQ Practice — always check indexOf != -1 before slicing.
09
Section · 09

Practice — attempt these now.

AP-style assessments aligned to this lesson. Time them.

FRQ Practice 30 min 17 marks Pending

AP CSA Unit 12 Topic 04 indexOf and substring Parsing FRQ Practice

AP-style topic practice assessment

Start