Choose Delphi line endings for UI text and file formats
Use sLineBreak for text whose line ending should follow the target platform. Use explicit #13#10 for a format requiring carriage return plus line feed, or #10 for a format requiring line feed alone. These choices describe different contracts, even when a text editor displays them identically.
In a Delphi Windows build, sLineBreak is CRLF. It is convenient for local multiline display text. A network message or generated file may instead require a fixed line-ending sequence regardless of the machine that produces it.
Inspect the bytes
Create a Delphi 13 Windows console application and replace its project source with the following program. It displays the encoded bytes, so the result does not depend on how your console renders line breaks.
program LineEndingDemo;
{$APPTYPE CONSOLE}
uses
System.SysUtils;
function HexBytes(const Bytes: TBytes): string;
var
Value: Byte;
begin
Result := '';
for Value in Bytes do
Result := Result + IntToHex(Value, 2);
end;
procedure Run;
var
WireText: string;
UnixText: string;
begin
WireText := 'A' + #13#10 + 'B' + #13#10;
UnixText := AdjustLineBreaks(WireText, tlbsLF);
Writeln('Platform break length=', Length(sLineBreak));
Writeln('CRLF bytes=', HexBytes(TEncoding.UTF8.GetBytes(WireText)));
Writeln('LF bytes=', HexBytes(TEncoding.UTF8.GetBytes(UnixText)));
Writeln('CRLF restored=', BoolToStr(AdjustLineBreaks(UnixText, tlbsCRLF) = WireText, True));
end;
begin
Run;
end.
Expected output on Windows:
Platform break length=2
CRLF bytes=410D0A420D0A
LF bytes=410A420A
CRLF restored=True
The letters A and B are 41 and 42 in UTF-8. CR is 0D and LF is 0A. Both strings include a final line terminator. Removing that final terminator would change the byte sequence even though the visible content might appear unchanged.
Keep encoding and line endings separate
UTF-8 determines how characters become bytes. It does not decide whether your format uses CRLF, LF or a final newline. Specify both when producing machine-consumed data. Likewise, a byte order mark is a separate file-format choice.
When the exact output bytes matter, construct the intended text, encode it once and write those bytes using a byte-oriented API. Review any text writer's newline configuration and preamble behavior before using it. Calling WriteLn adds a terminator of its own, which is different from writing a string that already contains all required terminators.
Avoid replacing every LF with CRLF blindly. Existing CRLF sequences would become CRCRLF. AdjustLineBreaks understands existing CRLF pairs and can normalize mixed CR, LF and CRLF text to the requested style. Normalization still changes the data, so apply it only when the consuming format permits that change.
Match the consuming control or protocol
For a VCL label, the text must contain actual newline characters, and the control must have enough space to display the lines. A literal backslash followed by n is not a Delphi newline escape. Use sLineBreak or character-code literals in the string.
For HTML, ordinary newline characters in text do not necessarily produce visible line breaks because HTML rendering normally collapses whitespace. Use the appropriate markup or whitespace styling in that format. For JSON, let a JSON serializer escape newline characters rather than manually inserting raw control characters into a JSON string literal.
Test empty content, a single line, mixed existing endings and the final-newline policy. Compare bytes when generating a protocol payload or golden file. A visual inspection in an editor cannot reliably distinguish all of these cases, and automatically normalizing an imported file may break a signature or checksum that covers its original bytes.
References
Question provenance
This article addresses How do I include a newline character in a string in Delphi? , asked by Brendan 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