typestar

String helpers in Delphi

A seed-library label gets trimmed, recased, searched and rewritten.

program StringHelpers;
uses SysUtils;

var
  Raw, Tidy: string;
  Marker: Integer;
begin
  Raw := '  Marigold / Row 4  ';
  Tidy := Trim(Raw);            // Trim drops leading and trailing spaces
  WriteLn(Format('[%s] becomes [%s]', [Raw, Tidy]));
  WriteLn(UpperCase(Tidy), ' or ', LowerCase(Tidy));
  WriteLn(Format('length %d', [Length(Tidy)]));
  Marker := Pos('/', Tidy);     // Pos counts from 1, or 0 when missing
  WriteLn(Format('slash at %d, seed is %s',
    [Marker, Trim(Copy(Tidy, 1, Marker - 1))]));
  WriteLn(StringReplace(Tidy, 'Row', 'Bed', [rfReplaceAll]));
end.

How it works

  1. Trim(Raw) strips the padding spaces and Length(Tidy) then reports 16.
  2. UpperCase and LowerCase return recased copies, leaving Tidy untouched.
  3. Pos('/', Tidy) locates the separator for Copy, and StringReplace swaps Row for Bed.

Keywords and builtins used here

The run, in numbers

Lines
17
Characters to type
557
Tokens
135
Three-star pace
75 tpm

At the three-star pace of 75 tokens a minute, this run takes about 108 seconds.

Type this snippet

Step 3 of 3 in Types & strings, step 6 of 27 in Language basics.

← Previous Next →