Skip to main content

BreakString

Procedure BreakString(S: string; const aSep: string; var Head, Tail: string)

Example

procedure ScriptEvent(var Value: variant);
var
Head, Tail: string;
begin
BreakString('customer:1007', ':', Head, Tail);
Value := Head + ' / ' + Tail; // customer / 1007
end;

Usage

BreakString splits text around the first exact separator substring and returns the unsplit input as Head when no match exists.

Parameters

NameModeDescription
SinputSource text. It is passed by value and is not modified.
aSepinputCase-sensitive separator substring. It may contain multiple characters.
Headoutput through varText before the first match, or all of S when no match exists.
Tailoutput through varText after the first match, or an empty string when no match exists.

The procedure returns no value and overwrites both output variables on every call.

Additional Technical Info

BreakString splits S at the first exact occurrence of aSep. The separator itself is discarded. Text before it is written to Head, and text after the complete separator is written to Tail.

The example is fictional and source-reviewed only.

Exact results

Input situationHeadTail
'A=B=C', separator '=''A''B=C'
'=A', separator '=''''A'
'A=', separator '=''A'''
'ABC', separator '=''ABC'''
'ABC', empty separator'ABC'''

The implementation uses Delphi Pos, Copy and Length. Matching is literal, case-sensitive and not aware of quoting, escaping, regular expressions or token boundaries. Only the first match is used; subsequent separators stay in Tail.

Use BreakStringTail when a missing separator should leave the original text in Tail instead. Use BreakStringLast only for a single-character delimiter when the last occurrence is required.

Created 2026-07-15