DEV Community

Sadiul Hakim
Sadiul Hakim

Posted on

CSV Quotes Explained Simply with Java Example

Why Quotes are Needed in CSV

Quotes are used in CSV files to handle special cases where data contains:

  • Commas (the delimiter)
  • Line breaks
  • Quotes themselves

Basic Examples

1. Simple CSV (No quotes needed)

Id,Name,Salary
1,John,1000
2,Jane,2000
Enter fullscreen mode Exit fullscreen mode

2. CSV with Commas in Data (Quotes needed)

Id,Name,Salary,Department
1,"Smith, John",1000,"IT, Development"
2,"Doe, Jane",2000,"HR, Recruitment"
Enter fullscreen mode Exit fullscreen mode

The quotes tell the CSV parser: "Everything between these quotes is one field, even if it contains commas"

3. CSV with Quotes in Data (Escaped quotes)

Id,Name,Comment
1,John,"He said ""Hello World"""
2,Jane,"She replied ""Nice to meet you"""
Enter fullscreen mode Exit fullscreen mode

Double quotes "" inside quoted fields represent a single quote character.

Raw Java Parser Logic:

boolean inQuotes = false; // Track if we're inside quoted text

for (int i = 0; i < line.length(); i++) {
    char c = line.charAt(i);

    if (c == '"') {
        if (inQuotes && i < line.length() - 1 && line.charAt(i + 1) == '"') {
            // This is an escaped quote "" → "
            currentValue.append('"');
            i++; // Skip the next quote
        } else {
            // Toggle quote state (start or end of quoted section)
            inQuotes = !inQuotes;
        }
    } else if (c == ',' && !inQuotes) {
        // Only treat comma as delimiter if NOT inside quotes
        values.add(currentValue.toString());
        currentValue = new StringBuilder();
    } else {
        currentValue.append(c);
    }
}
Enter fullscreen mode Exit fullscreen mode

OpenCSV Configuration:

.withCSVParser(new com.opencsv.CSVParserBuilder()
    .withSeparator(',')    // Field separator
    .withQuoteChar('"')    // Character that wraps special fields
    .withEscapeChar('\\')  // Alternative escape character (optional)
    .build())
Enter fullscreen mode Exit fullscreen mode

Real-world Examples

Example 1: Address with commas

Without quotes (WRONG):

Name,Address
John,123 Main St,New York,NY
Enter fullscreen mode Exit fullscreen mode

→ Parsed as: "John", "123 Main St", "New York", "NY"

With quotes (CORRECT):

Name,Address
John,"123 Main St, New York, NY"
Enter fullscreen mode Exit fullscreen mode

→ Parsed as: "John", "123 Main St, New York, NY"

Example 2: Text with quotes

Input:

Id,Message
1,"She said ""I love CSV files"""
Enter fullscreen mode Exit fullscreen mode

Parsed as:

  • Field 1: "1"
  • Field 2: "She said ""I love CSV files""" becomes → "She said "I love CSV files""

Visual Guide

"This is one, field" , "Another field" , RegularField
│                    │  │             │  │
│                    │  │             │  └─ No quotes needed
│                    │  │             └─ Quote character
│                    │  └─ Comma inside quotes = part of data
│                    └─ Comma outside quotes = field separator  
└─ Quote starts a special field
Enter fullscreen mode Exit fullscreen mode

Key Points to Remember:

  1. Quotes protect special characters inside them from being treated as delimiters
  2. Double quotes "" inside quoted fields represent a single quote "
  3. When inQuotes = true, commas are treated as regular text, not separators
  4. OpenCSV handles this automatically - you just need to configure the quote character

Top comments (0)