EncodeLineBreaks
Function EncodeLineBreaks(aInputString: string; aReplace: string): string
Example
procedure ScriptEvent(var Value: variant);
begin
Value := EncodeLineBreaks('first' + #13 + #10 + 'second' + #10 + 'third', '{LF}');
// first{LF}second{LF}third
end;
Usage
EncodeLineBreaks replaces every CRLF pair and every remaining lone LF with an exact caller-supplied marker while leaving lone CR unchanged.
Parameters and result
| Item | Type | Description |
|---|---|---|
aInputString | string | Text whose line breaks are encoded. |
aReplace | string | Literal text substituted for each recognised CRLF or LF. Empty text removes them. |
| Result | string | Encoded text. |
Additional Technical Info
EncodeLineBreaks converts Windows CRLF pairs and Unix-style lone LF characters to the supplied marker. It returns a new string and leaves the input unchanged. A lone carriage return (#13) is not converted.
The example is fictional and source-reviewed only.
Two-pass implementation
The function applies Delphi StringReplace with rfReplaceAll twice:
- replace every
#13#10pair withaReplace; - replace every
#10still present withaReplace.
The ordering prevents a CRLF pair from being counted twice. It also means that a CRLF introduced by aReplace during the first pass is subject to the second pass: its LF is replaced, leaving its CR plus another copy of the marker. Avoid markers containing LF when a reversible encoding is required.
Matching is exact and case-sensitive. Existing lone CR characters remain in the result. The routine does not recognise Unicode line separator characters or any textual escape such as '\\n' unless that exact text is itself chosen as the marker.
For reversible transport, choose a marker that cannot naturally occur in the data and that contains neither CR nor LF, or escape marker occurrences separately. DecodeLineFeeds cannot distinguish an inserted marker from the same text that was already present.
Related entries
EncodeLineFeedsreplaces CRLF pairs only.RemoveLineBreaksremoves CRLF and remaining LF.
External references
Created 2026-07-15