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
Trim(Raw)strips the padding spaces andLength(Tidy)then reports 16.UpperCaseandLowerCasereturn recased copies, leavingTidyuntouched.Pos('/', Tidy)locates the separator forCopy, andStringReplaceswaps Row for Bed.
Keywords and builtins used here
CopyFormatIntegerLengthLowerCasePosStringReplaceTrimUpperCaseWriteLnbeginendprogramstringusesvar
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.
Step 3 of 3 in Types & strings, step 6 of 27 in Language basics.