Skip to content
String Library Deep
step 1/5

Reading — step 1 of 5

Learn

~2 min readStrings, Procedural Types, Variants

FreePascal's SysUtils and StrUtils units cover most string needs.

SysUtils essentials

  • Format('%s = %d', [name, value]) — printf-style
  • IntToStr(n), StrToInt(s), TryStrToInt(s, n)
  • FloatToStr(x), StrToFloat(s), FormatFloat('0.00', x)
  • UpperCase(s), LowerCase(s)
  • Trim(s), TrimLeft(s), TrimRight(s)
  • StringReplace(s, old, new, [rfReplaceAll, rfIgnoreCase])
  • Concat(s1, s2, ...)

Splitting and joining

FPC 3.x adds string helpers:

uses SysUtils;
var
    s: string;
    parts: TStringArray;
    word: string;
begin
    s := 'hello world foo bar';
    parts := s.Split([' ']);
    for word in parts do
        WriteLn(word);
end.

Manual split (older code):

uses Classes;
var lines: TStringList;
begin
    lines := TStringList.Create;
    try
        lines.Delimiter := ' ';
        lines.StrictDelimiter := True;
        lines.DelimitedText := s;
        for i := 0 to lines.Count - 1 do
            WriteLn(lines[i]);
    finally
        lines.Free;
    end;
end.

StrUtils — extras

uses StrUtils;

DupeString('ab', 3)              { 'ababab' }
LeftStr('hello', 3)              { 'hel' }
RightStr('hello', 3)             { 'llo' }
MidStr('hello', 2, 3)            { 'ell' }
ReverseString('hello')           { 'olleh' }
ContainsText(haystack, needle)   { case-insensitive }
StartsText('foo', 'foo bar')     { true }

Pos and PosEx

Pos('world', 'hello world')      { 7 — 1-indexed }
Pos('xyz', 'hello world')        { 0 — not found }
PosEx('o', 'foo', 1)             { find 'o' starting at index 1 }

String concatenation in loops

Avoid s := s + chunk in loops — copies grow. Use TStringBuilder:

uses Classes, SysUtils;
var sb: TStringBuilder;
begin
    sb := TStringBuilder.Create;
    try
        for i := 1 to 1000 do
            sb.Append(IntToStr(i));
        WriteLn(sb.ToString);
    finally
        sb.Free;
    end;
end.

Format specifiers

Format('%d items', [42])             { '42 items' }
Format('%6.2f%%', [99.5])            { ' 99.50%' }
Format('%-10s|', ['hi'])             { 'hi        |' (left-pad) }
Format('%s = %d', ['x', 5])

The [ ... ] is an open-array of Variant (any type). Format infers from %d, %s, %f.

Conversion best practices

uses SysUtils;
var n: integer;
begin
    if TryStrToInt(input, n) then
        Process(n)
    else
        WriteLn('invalid: ', input);
end.

TryStrToInt/TryStrToFloat return boolean instead of throwing — much nicer for input parsing.

Discussion

Ask a question, share an insight, or help someone who’s stuck.

Sign in to post a comment or reply.

Loading…