Specialised String Routines

You can see the many string routines available in the Lazarus documentation of SysUtils and StrUtils. Also, the interface section of the source code for each unit provides useful information about the use of the routines. For Delphi users, the routines are well documented in the help files. As usual, DelphiBasics is helpful.

The following program illustrates the use of ansiReplaceStr and in the section below it we provide examples of the use of placeholders by the Format function.

program ReplaceDemo;
  {$APPTYPE CONSOLE}
uses
  SysUtils, StrUtils;
var
  TestString, SearchString, ReplaceString: string;
begin
  writeln('Here is one we prepared earlier:', #13#10);
  TestString := 'The quick brown fox jumped over the lazy dog.';
  SearchString := 'lazy';
  ReplaceString := 'motivationally challenged';
  TestString := ansiReplaceStr(TestString, SearchString, ReplaceString);
  writeln(TestString);
  writeln;
  writeln('Here is one for you to try.  The starting sentence is:');
  TestString := 'Manchester United will win the Premiership next year.';
  writeln(TestString);
  write('Please enter the string to be replaced. ');
  readln(SearchString);
  write('Please enter the string to replace ',SearchString, ' with. ');
  readln(ReplaceString);
  TestString := ansiReplaceStr(TestString, SearchString, ReplaceString);
  writeln(TestString);
  readln;
end.  

The Format Function

The format function is powerful and many examples are provided on DelphiBasics. Our first little Lazarus demonstration shows the use of placeholders for strings.

program FormatString;
uses
 SysUtils;

procedure WeatherForecast(Day, Weather: string);
begin
  writeln(format('The weather on %s will be %s.', [Day, Weather]));
end;

begin
  WeatherForecast('Monday', 'cold and wet');
  WeatherForecast('Tuesday', 'warm and wet');
  WeatherForecast('Wednesday', 'warm and windy');
  readln;
end.      

The output is:
The weather on Monday will be cold and wet.
The weather on Tuesday will be warm and wet.
The weather on Wednesday will be warm and windy.

The next example has a placeholder for an integer.

program FormatInteger;
uses
 SysUtils;

procedure Population(Country: string; Population: integer);
begin
  writeln(format('The population of %s in 2011 as measured in the census was approximately %d.', [Country, Population]));
end;

begin
  Population('England', 53000000);
  Population('Scotland', 5300000);
  readln;
end.    

The output is:
The population of England in 2011 as measured in the census was approximately 53000000.
The population of Scotland in 2011 as measured in the census was approximately 5300000.

The final example uses a placeholder for a real (floating point) number, formatted to display to two places of decimals.

program FormatFloat;
uses
 SysUtils;

procedure Divide(Dividend, Divisor: integer);
var
  Result: real;
begin
  Result := Dividend / Divisor;
  writeln(format('The result of %d divided by %d is %.2f.', [Dividend, Divisor, Result]));
end;

begin
  Divide(4, 3);
  Divide(5, 6);
  readln;
end.     

The output is:
The result of 4 divided by 3 is 1.33.
The result of 5 divided by 6 is 0.83.

In the next section we demonstrate the use of:

Programming - a skill for life!

Introduction to string routines such as the concat function