01
Overview

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.

02
Core Concept

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.

03
Key Vocabulary

CSV / delimiter vocabulary.

Used in structured-data problems.

Vocabulary
  • 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.
04
AP Exam Focus

How CSV parsing is tested.

Counting and the "remaining" pattern dominate.

Exam Focus
  • 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.
05
Worked Example

Parse "apple,banana,cherry".

Shrink remaining; extract fields.

Worked Example AP-style reasoning

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"].

06
Common Mistakes

CSV-parsing pitfalls.

Most miss the last field or off-by-one.

Watch Out
  • 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.
07
Reference Table

CSV parsing template.

Count, then extract.

CSV Reference

AP Quick Reference
StepCodeNote
Count commasScan for ','Field count = commas + 1.
Find next commaindexOf(',')In remaining.
Extract fieldsubstring(0, idx)Before comma.
Update remainingsubstring(idx + 1)Skip comma.
Last fieldWhat's left after loopNo comma after.
08
Practice Tip

How to master CSV parsing.

Always remember the last field after the loop.

Practice Strategy
  • 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.
09
Section · 09

Practice — attempt these now.

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

FRQ Practice 30 min 19 marks Pending

AP CSA Unit 12 Topic 10 Delimiter Counting and Field Extraction FRQ Practice

AP-style topic practice assessment

Start