This chapter describes the STRINGS unit for Free Pascal. This unit is system independent, and therefore works on all supported platforms.
Since the unit only provides some procedures and functions, there is only one section, which gives the declarations of these functions, together with an explanation.
Program Example11;
Uses strings;
{ Program to demonstrate the StrCat function. }
Const P1 : PChar = 'This is a PChar String.';
Var P2 : PChar;
begin
P2:=StrAlloc (StrLen(P1)*2+1);
StrMove (P2,P1,StrLen(P1)+1); { P2=P1 }
StrCat (P2,P1); { Append P2 once more }
Writeln ('P2 : ',P2);
end.
For an example, see StrLComp.
Program Example4;
Uses strings;
{ Program to demonstrate the StrCopy function. }
Const P : PCHar = 'This is a PCHAR string.';
var PP : PChar;
begin
PP:=StrAlloc(Strlen(P)+1);
STrCopy (PP,P);
If StrComp (PP,P)<>0 then
Writeln ('Oh-oh problems...')
else
Writeln ('All is well : PP=',PP);
end.
Program Example17;
Uses strings;
{ Program to demonstrate the StrDispose function. }
Const P1 : PChar = 'This is a PChar string';
var P2 : PChar;
begin
Writeln ('Before StnNew : Memory available : ',MemAvail);
P2:=StrNew (P1);
Writeln ('After StrNew : Memory available : ',MemAvail);
Writeln ('P2 : ',P2);
StrDispose(P2);
Writeln ('After StrDispose : Memory available : ',MemAvail);
end.
Program Example6;
Uses strings;
{ Program to demonstrate the StrECopy function. }
Const P : PChar = 'This is a PCHAR string.';
Var PP : PChar;
begin
PP:=StrAlloc (StrLen(P)+1);
If Longint(StrECopy(PP,P))-Longint(PP)<>StrLen(P) then
Writeln('Something is wrong here !')
else
Writeln ('PP= ',PP);
end.
Program Example6;
Uses strings;
{ Program to demonstrate the StrEnd function. }
Const P : PChar = 'This is a PCHAR string.';
begin
If Longint(StrEnd(P))-Longint(P)<>StrLen(P) then
Writeln('Something is wrong here !')
else
Writeln ('All is well..');
end.
Program Example8;
Uses strings;
{ Program to demonstrate the StrLComp function. }
Const P1 : PChar = 'This is the first string.';
P2 : PCHar = 'This is the second string.';
Var L : Longint;
begin
Write ('P1 and P2 are ');
If StrComp (P1,P2)<>0 then write ('NOT ');
write ('equal. The first ');
L:=1;
While StrLComp(P1,P2,L)=0 do inc (L);
dec(l);
Writeln (l,' characters are the same.');
end.
Program Example12;
Uses strings;
{ Program to demonstrate the StrLCat function. }
Const P1 : PChar = '1234567890';
Var P2 : PChar;
begin
P2:=StrAlloc (StrLen(P1)*2+1);
P2^:=#0; { Zero length }
StrCat (P2,P1);
StrLCat (P2,P1,5);
Writeln ('P2 = ',P2);
end.
Program Example8;
Uses strings;
{ Program to demonstrate the StrLComp function. }
Const P1 : PChar = 'This is the first string.';
P2 : PCHar = 'This is the second string.';
Var L : Longint;
begin
Write ('P1 and P2 are ');
If StrComp (P1,P2)<>0 then write ('NOT ');
write ('equal. The first ');
L:=1;
While StrLComp(P1,P2,L)=0 do inc (L);
dec(l);
Writeln (l,' characters are the same.');
end.
Program Example5;
Uses strings;
{ Program to demonstrate the StrLCopy function. }
Const P : PCHar = '123456789ABCDEF';
var PP : PCHar;
begin
PP:=StrAlloc(11);
Writeln ('First 10 characters of P : ',StrLCopy (PP,P,10));
end.
Program Example1;
Uses strings;
{ Program to demonstrate the StrLen function. }
Const P : PChar = 'This is a constant pchar string';
begin
Writeln ('P : ',p);
Writeln ('length(P) : ',StrLen(P));
end.
For an example, see StrIComp
Program Example14;
Uses strings;
{ Program to demonstrate the StrLower and StrUpper functions. }
Const
P1 : PChar = 'THIS IS AN UPPERCASE PCHAR STRING';
P2 : PChar = 'this is a lowercase string';
begin
Writeln ('Uppercase : ',StrUpper(P2));
StrLower (P1);
Writeln ('Lowercase : ',P1);
end.
Program Example10;
Uses strings;
{ Program to demonstrate the StrMove function. }
Const P1 : PCHAR = 'This is a pchar string.';
Var P2 : Pchar;
begin
P2:=StrAlloc(StrLen(P1)+1);
StrMove (P2,P1,StrLen(P1)+1); { P2:=P1 }
Writeln ('P2 = ',P2);
end.
Program Example16;
Uses strings;
{ Program to demonstrate the StrNew function. }
Const P1 : PChar = 'This is a PChar string';
var P2 : PChar;
begin
P2:=StrNew (P1);
If P1=P2 then
writeln ('This can''t be happening...')
else
writeln ('P2 : ',P2);
end.
Program Example3;
Uses strings;
{ Program to demonstrate the StrPas function. }
Const P : PChar = 'This is a PCHAR string';
var S : string;
begin
S:=StrPas (P);
Writeln ('S : ',S);
end.
Program Example2;
Uses strings;
{ Program to demonstrate the StrPCopy function. }
Const S = 'This is a normal string.';
Var P : Pchar;
begin
p:=StrAlloc (length(S)+1);
if StrPCopy (P,S)<>P then
Writeln ('This is impossible !!')
else
writeln (P);
end.
Program Example15;
Uses strings;
{ Program to demonstrate the StrPos function. }
Const P : PChar = 'This is a PChar string.';
S : Pchar = 'is';
begin
Writeln ('Position of ''is'' in P : ',longint(StrPos(P,S))-Longint(P));
end.
For an example, see StrScan.
Program Example13;
Uses strings;
{ Program to demonstrate the StrScan and StrRScan functions. }
Const P : PChar = 'This is a PCHAR string.';
S : Char = 's' ;
begin
Writeln ('P, starting from first ''s'' : ',StrScan(P,s));
Writeln ('P, starting from last ''s'' : ',StrRScan(P,s));
end.
For an example, see StrLower