Pascal Tips Homepage  •  Privacy Policy & Imprint

FreePascal Unit SysUtils

Formatting Strings

Reference of the string-formatting functions from the SysUtils unit, section Formatting Strings.
Every function name links to its original documentation page.

AdjustLineBreaks

function AdjustLineBreaks(const S: string): string;
function AdjustLineBreaks(const S: string; Style: TTextLineBreakStyle): string;

Replaces every occurrence of #13 and #10 in S with the line-ending sequence appropriate for the current platform (#13#10 on Windows/DOS, #10 on Unix-like systems, #13 on classic Mac OS); the overload with Style lets the caller force a specific line-ending convention instead of the platform default.

FormatBuf

function FormatBuf(var Buffer; BufLen: Cardinal; const Fmt; fmtLen: Cardinal; const Args: array of Const): Cardinal;
function FormatBuf(var Buffer; BufLen: Cardinal; const Fmt; fmtLen: Cardinal; const Args: array of Const; const FormatSettings: TFormatSettings): Cardinal;

Formats the Fmt buffer (of length fmtLen) with the given Args, exactly as Format does, and stores up to BufLen bytes of the result in Buffer; returns the number of bytes written.

Format

function Format(const Fmt: string; const Args: array of Const): string;
function Format(const Fmt: string; const Args: array of Const; const FormatSettings: TFormatSettings): string;

Replaces every placeholder of the form %[Index:][-][Width][.Precision]ArgType in Fmt with the corresponding element from Args and returns the resulting string; Index selects which argument to use (default the next one in sequence), '-' left-aligns padded output, Width sets a minimum field width, and Precision refines how the value is rendered depending on ArgType. Raises EConvertError on a malformed format string, a type mismatch with the next argument, or too few arguments.

TypeMeaning
DDecimal integer, zero-padded to at least Precision digits if given.
EScientific notation via FloatToStrF(Argument, ffExponent, Precision, 3).
FFixed-point notation; Precision sets the number of decimal digits.
GGeneral number format: fixed or scientific, whichever is shorter.
MCurrency format (fixed-point plus currency symbol).
NLike fixed-point, but with thousand separators inserted.
PPointer value as an 8-character hexadecimal string.
SString argument, optionally truncated to Precision characters.
UUnsigned decimal integer, zero-padded to at least Precision digits if given.
XHexadecimal integer, padded to at least Precision characters (max 32).

FmtStr

procedure FmtStr(var Res: string; const Fmt: string; const args: array of Const);
procedure FmtStr(var Res: string; const Fmt: string; const args: array of Const; const FormatSettings: TFormatSettings);

Calls Format with Fmt and args and stores the resulting string in Res; raises EConvertError on the same errors as Format.

QuotedStr

function QuotedStr(const S: string): string;

Returns S enclosed in single quotes, with every embedded single quote doubled; equivalent to calling AnsiQuotedStr(S, '''').

StrFmt

function StrFmt(Buffer: PChar; Fmt: PChar; const args: array of Const): Pchar;
function StrFmt(Buffer: PChar; Fmt: PChar; const Args: array of Const; const FormatSettings: TFormatSettings): PChar;

Formats Fmt with args as Format does and stores the result in Buffer, returning Buffer; Buffer must point to enough space to hold the entire result.

StrLFmt

function StrLFmt(Buffer: PCHar; Maxlen: Cardinal; Fmt: PChar; const args: array of Const): Pchar;
function StrLFmt(Buffer: PCHar; Maxlen: Cardinal; Fmt: PChar; const args: array of Const; const FormatSettings: TFormatSettings): Pchar;

Formats Fmt with args as Format does, but writes at most Maxlen characters of the result into Buffer, returning Buffer; Buffer must have room for Maxlen characters.

TrimLeft

function TrimLeft(const S: string): string;
function TrimLeft(const S: widestring): widestring;
function TrimLeft(const S: unicodestring): unicodestring;

Strips blank characters (all characters with an ordinal value of 32 or less) from the beginning of S and returns the result; returns an empty string if S consists only of such characters.

TrimRight

function TrimRight(const S: string): string;
function TrimRight(const S: widestring): widestring;
function TrimRight(const S: unicodestring): unicodestring;

Strips blank characters (all characters with an ordinal value of 32 or less) from the end of S and returns the result; returns an empty string if S consists only of such characters.

Trim

function Trim(const S: string): string;
function Trim(const S: widestring): widestring;
function Trim(const S: unicodestring): unicodestring;

Strips blank characters (all characters with an ordinal value of 32 or less) from both the beginning and end of S and returns the result; returns an empty string if S consists only of such characters.


Other routines in SysUtils: File Name Handling Routines, File Input/Output Routines, Date/Time Routines, Conversion Routines, String Functions, Formatting Strings, PChar Related Functions.