Counting delimiters and extracting CSV fields.
When data arrives as comma-separated values, two skills handle it: counting the commas and slicing the fields between them.
A delimiter is a character that separates fields — commas in CSV data, slashes in dates, hyphens in IDs. Counting them tells you how many fields you have; tracking their positions lets you extract each field.
The number of fields in a row equals commaCount + 1 (assuming no leading/trailing commas).
On the AP exam, delimiter-based parsing appears in CSV FRQs and date/format problems.
Count by scanning; extract by indexOf + substring loop.
One scan counts; another sequence of indexOf calls extracts.
Counting commas
public int countCommas(String s) {
int count = 0;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == ',') count++;
}
return count;
}
Field count from comma count
If a CSV row has 3 commas, it has 4 fields. (Edge cases: empty string has 1 empty field; leading/trailing commas add empty fields.)
Extracting one field at a time
public String firstField(String csv) {
int commaIndex = csv.indexOf(',');
if (commaIndex == -1) return csv; // no comma; whole string is the only field
return csv.substring(0, commaIndex);
}
public String afterFirstComma(String csv) {
int commaIndex = csv.indexOf(',');
if (commaIndex == -1) return "";
return csv.substring(commaIndex + 1);
}
Repeated application of afterFirstComma peels off one field per call.
The "remaining" pattern
String remaining = csv;
while (remaining.indexOf(',') != -1) {
int idx = remaining.indexOf(',');
String field = remaining.substring(0, idx);
// process field
remaining = remaining.substring(idx + 1);
}
// last field has no comma
String lastField = remaining;
Each iteration takes one field and shrinks remaining by that field plus its trailing comma.
CSV / delimiter vocabulary.
Used in structured-data problems.
- Delimiter — character separating fields.
- CSV — Comma-Separated Values.
- Field — one value between delimiters.
- Field count — delimiters + 1 (typically).
- Remaining — unparsed tail of the String.
- Trailing comma — comma at the end indicating an empty final field.
How CSV parsing is tested.
Counting and the "remaining" pattern dominate.
- Fields = commas + 1. Standard formula.
- Shrink remaining each step. Don't re-search from the start.
- Handle no-comma case. Single field.
- Last field has no trailing comma. Loop exits with it as remaining.
Parse "apple,banana,cherry".
Shrink remaining; extract fields.
Input: "apple,banana,cherry".
Comma count: 2.
Field count: 3.
Extraction trace:
- remaining = "apple,banana,cherry"; idx = 5; field = "apple"; remaining = "banana,cherry".
- remaining = "banana,cherry"; idx = 6; field = "banana"; remaining = "cherry".
- remaining has no comma; loop exits; lastField = "cherry".
All fields: ["apple", "banana", "cherry"].
CSV-parsing pitfalls.
Most miss the last field or off-by-one.
- Forgetting the last field. Loop exits when no comma; last field is what's left in remaining.
- Including the delimiter in a field. Use
substring(idx + 1)to skip the comma. - Off-by-one in field count. Commas + 1, not just commas.
- Not updating remaining. Infinite loop.
CSV parsing template.
Count, then extract.
CSV Reference
AP Quick Reference| Step | Code | Note |
|---|---|---|
| Count commas | Scan for ',' | Field count = commas + 1. |
| Find next comma | indexOf(',') | In remaining. |
| Extract field | substring(0, idx) | Before comma. |
| Update remaining | substring(idx + 1) | Skip comma. |
| Last field | What's left after loop | No comma after. |
How to master CSV parsing.
Always remember the last field after the loop.
- MCQ Practice — count commas; predict field count; verify each substring boundary.
- Java Lab — write countCommas, getNthField, getAllFields. Test on no-comma, single-field, and trailing-comma input.
- FRQ Practice — use the "shrink remaining" pattern; handle the last field outside the loop.
Practice — attempt these now.
AP-style assessments aligned to this lesson. Time them.
AP CSA Unit 12 Topic 10 Delimiter Counting and Field Extraction FRQ Practice
AP-style topic practice assessment