Skip to main content

SplitString

Procedure SplitString( const aString : String; const aSeparator : Char; var aStrings : TStringArray)

Example

procedure ScriptEvent(var Value: variant);
var
Parts: TStringArray;
begin
SplitString('North||South|', '|', Parts);
Value := Parts[1]; // South; the empty fields are discarded
end;

Usage

SplitString splits trimmed text on one separator character, discarding every empty field and replacing the output array.

Important behavior and quirks

  • Leading, trailing and repeated separators do not create empty array elements.
  • Empty or whitespace-only input produces an empty array.
  • Trimming applies only to the outside of the complete input. With comma separation, ' A , B ' becomes tokens 'A ' and ' B'; token-adjacent spaces remain.
  • If the separator itself is an ordinary space, leading/trailing spaces disappear and runs of spaces collapse because empty tokens are discarded.
  • The procedure does not understand quotes, escapes, CSV rules or nested syntax.
  • aStrings is destructive output: its previous contents are released before parsing.
  • Embedded NUL is handled as an ordinary length-counted character unless NUL is the chosen separator.

Additional Technical Info

SplitString replaces aStrings with the non-empty tokens found by splitting aString on one exact Char. It trims the complete input before splitting, but it does not trim each individual token.

The example is fictional and source-reviewed only.

Parameters and mutation

ParameterMeaning
aStringInput text. A Delphi Trim is applied to the whole value.
aSeparatorOne exact separator character. There is no multi-character delimiter or character-set interpretation.
aStringsOutput array. Its length is set to zero before scanning and it is expanded once for each non-empty token.

Exact algorithm

Velox computes Trim(aString) + aSeparator, then scans from position 1. Non-separator characters are appended to a temporary token. On a separator, the token is appended only when it is non-empty; otherwise the separator is simply skipped. The appended sentinel separator flushes the final token.

Performance and concurrency

The implementation grows both the current token and result array incrementally. Long tokens or many fields can cause repeated allocations and copies. It is stateless apart from the caller-owned output array.

Related entries

  • StringToToken - tokenises using a separator set into a TStringList.
  • ProperCase - uses this exact splitter with an ordinary-space separator.

External references

Created 2026-07-15