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.
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)— fromstartto end, inclusive of start.s.substring(start, end)— fromstartup to but NOT includingend.- 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".
Parsing vocabulary.
Used in structured-input problems.
- 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.
How indexOf + substring is tested.
Off-by-one and missing delimiter dominate.
- 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 + 1for the right side. - Empty substring possible.
substring(0, 0)returns "".
Parse first and last name.
Split on space.
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.
Parsing pitfalls.
Index arithmetic is the top issue.
- Including the delimiter in the right side. Forgetting
+ 1includes 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.
Parsing template.
Find, guard, cut.
indexOf + substring Reference
AP Quick Reference| Operation | Code | Result |
|---|---|---|
| Find delimiter | indexOf(d) | Position or -1. |
| Left side | substring(0, i) | Before delimiter. |
| Right side | substring(i + 1) | After delimiter. |
| No delimiter | Check for -1 | Return default. |
How to master parsing.
Guard first; then cut.
- 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 != -1before slicing.
Practice — attempt these now.
AP-style assessments aligned to this lesson. Time them.
AP CSA Unit 12 Topic 04 indexOf and substring Parsing FRQ Practice
AP-style topic practice assessment