NASutils Knowledge BaseUnofficialPolski

Pascal · lesson 5 of 30 · First stepsWork with text

Length, Copy, Pos and the tools of the unit SysUtils work on text. With {$H+} a string can be longer than 255 bytes.

Programming on the NAS › Pascal in 30 lessons

05-text.pas Download 05-text.pas

program TextTools;
{$mode objfpc}{$H+}
uses
  SysUtils;
var
  FileName: string;
begin
  FileName := 'Holiday-2027.JPG';
  WriteLn('length: ', Length(FileName));
  WriteLn('first 7: ', Copy(FileName, 1, 7));
  WriteLn('the dash is at ', Pos('-', FileName));
  WriteLn('small letters: ', LowerCase(FileName));
  WriteLn('ending: ', ExtractFileExt(FileName));
  WriteLn('new ending: ', ChangeFileExt(FileName, '.txt'));
  WriteLn('replaced: ', StringReplace(FileName, '2027', '2028', []));
  WriteLn('first letter: ', FileName[1]);
end.

Terminal

$ ./05-text
length: 16
first 7: Holiday
the dash is at 8
small letters: holiday-2027.jpg
ending: .JPG
new ending: Holiday-2027.txt
replaced: Holiday-2028.JPG
first letter: H

Exercise

Print the name of the file in capital letters.