Basic String Routines

Commonly used functions are described in the following table. In the table, Str, Str1 and Str2 are strings and n and m are positive integers. The first character is numbered 1 in Pascal. Thus, the position of ‘g’ in the string ‘program’ is 4.

Function Result
Pos(Str1, Str2) Returns the start position of Str1 in Str2. Returns 0 if Str1 is not in Str2
LeftStr(Str, n) Returns the first n characters of Str as a string
MidStr(Str, m, n) Returns, as a string, the next n characters of Str, starting at the mth character
RightStr(Str, n) Returns the last n characters of Str
Length(Str) Returns the number of characters in Str

The following statements demonstrate the use of these functions.

intP := pos('gram', 'programming');  // intP becomes 4
strL := leftStr('programming', 7);   // strL becomes 'program'
strM := midStr('programming', 5, 3); // strM becomes 'ram'
strR := rightStr('programming', 4);  // strR becomes 'ming'
intL := length('programming');       // intL becomes 11

It is important to know that all these functions are case sensitive:

intP := pos('Gram', 'programming');  //intP becomes 0 

Copy has the same result as midStr.

strM := copy('programming', 5, 3);   // strM becomes 'ram'

In order to obtain a single letter in a string, e.g. the fourth letter in the variable MyString, it is neater to use MyString[4] than MidStr(MyString,4,1). Indeed, by treating a string as an array of char, you can develop code that will reproduce the results of the routines covered in this tutorial. See the format check in the validation tutorial for an example. There are further examples in the next section.

Both Lazarus and Delphi have many string routines in the SysUtils unit and others in the StrUtils unit. Note that LeftStr and RightStr are in SysUtils in Lazarus but in StrUtils in Delphi.

Program StringDemo demonstrates the use of leftStr, rightStr, midStr and pos. In addition, it introduces the functions lowerCase and upperCase for use with strings and the function upCase for use with chars. The program also includes two of the many string conversion functions. The function strToInt (for converting strings to integers) is widely used. The function intToHex converts an integer to a string representing its value in hexadecimal format.

program StringDemo;
  {$APPTYPE CONSOLE}
uses
  SysUtils, StrUtils, DateUtils;
var
  Forename : string;
  Choice : char;

procedure Name;
var
  Fullname, Surname : string;
  SpacePos : integer;
begin
  write('Please enter your forename and surname separated by one space. ');
  readln(Fullname);
  SpacePos := pos(' ', Fullname);
  Forename := leftStr(Fullname, SpacePos-1);
  Surname := rightStr(FullName, Length(Fullname)-SpacePos);
  Surname := lowerCase (Surname);
  writeln('Your forename: ', Forename);
  writeln('Your surname in lower case: ', Surname);
  Surname[1] := upCase(Surname[1]);
  writeln('Your surname in mixed case: ', Surname);
  Surname := upperCase(Surname);
  writeln('Your surname in upper case: ', Surname);
end;

procedure Birthday;
var
  DayOfMonth, Month, Year : integer;
  strDate : string;
begin
  repeat
    write('Please enter your date of birth using the format --/--/----. ');
    readln(strDate);
    DayOfMonth := strToInt(leftStr(strDate, 2));
    Month := strToInt(midStr(strDate, 4, 2));
    Year := strToInt(rightStr(strDate, 4));
  until isValidDate(Year, Month, DayofMonth);
  writeln(Forename,', you were born on day ', DayOfMonth, ' of month ',
          Month, ' in the year ', Year, '.');
  writeln('In hex that is day ', intToHex(DayOfMonth,2), ' of month ',
           intToHex(Month,1), ' in the year ', intToHex(Year,3), '!');
end;

begin
  Name;
  Birthday;
  readln;
end.
Programming - a skill for life!

Introduction to string routines such as the concat function