Split Delphi text with explicit empty-field and quoting rules
Choose a splitting operation only after defining the format. A comma can be an unconditional separator, or it can be ordinary data inside a quoted field. Empty fields may represent missing values, while spaces may be meaningful characters rather than extra separators.
Delphi's string helper supports direct separator splitting. TStringList.DelimitedText provides a different delimited-text model with a delimiter and quote character. Its default space handling can surprise callers, so set StrictDelimiter deliberately.
Split simple fields
Create a Delphi 13 console application and replace the project source with this program. The first input has no quoting syntax. It preserves the empty field between adjacent delimiters and the final empty field.
program SplitDemo;
{$APPTYPE CONSOLE}
uses
System.SysUtils,
System.Classes;
procedure Run;
var
Input: string;
Parts: TArray<string>;
Part: string;
Fields: TStringList;
Index: Integer;
begin
Input := 'red,,blue,';
Parts := Input.Split([','], TStringSplitOptions.None);
Writeln('Simple count=', Length(Parts));
for Part in Parts do
Writeln('[', Part, ']');
Fields := TStringList.Create;
try
Fields.Delimiter := ',';
Fields.QuoteChar := '"';
Fields.StrictDelimiter := True;
Fields.DelimitedText := '"North, East",two words,"a ""quote""",';
Writeln('Quoted count=', Fields.Count);
for Index := 0 to Fields.Count - 1 do
Writeln('[', Fields[Index], ']');
finally
Fields.Free;
end;
end;
begin
Run;
end.
Expected output:
Simple count=4
[red]
[]
[blue]
[]
Quoted count=4
[North, East]
[two words]
[a "quote"]
[]
TStringSplitOptions.None preserves the simple empty fields. ExcludeEmpty would remove them, which changes column positions and can change the meaning of structured data. Use that option only when missing positions are intentionally irrelevant.
Parse quoted fields deliberately
The second input contains a comma inside quotes, an unquoted space, a doubled quote and a trailing delimiter. StrictDelimiter := True keeps the space in two words. QuoteChar still controls quoted fields and doubled quote characters.
This is a useful parser for the stated Delphi delimited-text convention. Do not assume it validates every CSV dialect. File formats may permit embedded newlines, require a specific escape convention, impose a fixed field count or reject unmatched quotes. Define those rules and use a parser that enforces them when reading external CSV files.
The string helper also has quote-aware overloads, but changing overloads can change which characters are retained and how empty fields are handled. Keep a small fixture containing every feature your chosen format uses. Passing the ordinary comma example alone does not validate quote behavior.
Validate after tokenization
Splitting produces strings, not typed records. Check the number of fields, required values, numeric formats and allowed ranges before accepting the row. Keep error messages tied to the original row and field position so a malformed import can be corrected.
Do not trim every field automatically. Leading and trailing spaces may be significant in a key, a display name or a quoted value. If your import contract permits trimming, apply it at that boundary and retain the original input when auditability matters.
For large files, distinguish one record from the entire document. A line-based reader is insufficient if quoted fields can contain line breaks. Conversely, loading the whole file into one DelimitedText value does not establish a row structure. Use bounded reading and a parser whose record model matches the file format.
Test empty input, adjacent separators, a trailing separator, a quoted delimiter, a doubled quote and malformed quoting. Treat the displayed expected output as the contract for this example, and verify it against your supported Delphi version before replacing an existing parser.
References
Question provenance
This article addresses Split a string into an array of strings based on a delimiter , asked by Ryan on Stack Overflow. The source question is licensed under CC BY-SA 4.0 . The explanation and examples here are newly authored for current Delphi. No answer text or code is reproduced.
Author: Herman Schoenfeld