{csv:text:char}

Description

Escapes a single value so it is safe to place inside one field of a CSV (comma-separated values) row. If the value contains the separator character, a double quote, or a line break, csv wraps the whole value in double quotes and doubles every internal double quote (the RFC 4180 rule). A value with none of those characters is returned unchanged. The second parameter sets the separator character to test for; it must be exactly one character, otherwise a comma is used. Build a whole CSV row by escaping each field with csv and joining the results with your separator. Leading and trailing whitespace is trimmed from the value before escaping.

Parameters

text required

The single value to escape (one field of a CSV row). Typically a field-getter that reads one item field.

char optional default , (comma)

The separator character to test for. csv quotes the value only when it contains this character (or a double quote or a line break). Must be exactly one character; any other value falls back to a comma. Use a semicolon for semicolon-separated CSV.

Examples

test[{csv:hello world}]
Expected[hello world]
Actual[hello world]
A value with no separator, double quote, or line break is returned exactly as given - no quoting added.
test[{csv:Doe, John}]
Expected["Doe, John"]
Actual["Doe, John"]
When the value contains the separator (here the default comma), csv wraps the whole value in double quotes so the comma is not read as a field break.
test[{csv:she said "hi"}]
Expected["she said ""hi"""]
Actual["she said ""hi"""]
A double quote inside the value is escaped by doubling it, and the whole value is wrapped in quotes - the standard CSV rule.
test[{csv:Doe; John:;}]
Expected["Doe; John"]
Actual["Doe; John"]
Pass a semicolon as the second parameter for semicolon-separated CSV. Now a value containing a semicolon gets quoted.
test[{csv:a;b}]
Expected[a;b]
Actual[a;b]
csv only quotes for the separator you ask for. Under the default comma, a semicolon is an ordinary character, so the value is left unquoted. Match the separator to how you join the row.
test{csv:Doe; John:;};{csv:editor:;};{csv:Prague:;}
Expected"Doe; John";editor;Prague
Actual"Doe; John";editor;Prague
The real-world pattern: escape each field with csv using your separator, then join the escaped results with that same separator. Here three fields are joined with semicolons; only the first needs quoting.
test[{csv:}]
Expected[]
Actual[]
An empty value produces empty output (no quotes) - it still occupies its column in the row.