Pascal - Strings

The string in Pascal is actually a sequence of characters with an optional size specification. The characters could be numeric, letters, blank, special characters or a combination of all. Extended Pascal provides numerous types of string objects depending upon the system and implementation. We will discuss more common types of strings used in programs.
You can define a string in many ways −
Character arrays − This is a  character string which is a sequence of zero or more byte-sized characters  enclosed in single quotes.
Character arrays − This is a  character string which is a sequence of zero or more byte-sized characters  enclosed in single quotes.
String variables − The variable of String type, as defined in Turbo Pascal.
String variables − The variable of String type, as defined in Turbo Pascal.
Short strings − The variable of String type with size specification.
Short strings − The variable of String type with size specification.
Null terminated strings − The variable of pchar type.
Null terminated strings − The variable of pchar type.
AnsiStrings − Ansistrings are strings that have no length limit.
AnsiStrings − Ansistrings are strings that have no length limit.
Pascal provides only one string operator, string concatenation operator (+).
The following program prints first four kinds of strings. We will use AnsiStrings in the next example.
program exString;
var
   greetings: string;
   name: packed array [1..10] of char;
   organisation: string[10];
   message: pchar;

begin
   greetings := 'Hello ';
   message := 'Good Day!';
   
   writeln('Please Enter your Name');
   readln(name);
   
   writeln('Please Enter the name of your Organisation');
   readln(organisation);
   
   writeln(greetings, name, ' from ', organisation);
   writeln(message); 
end.
When the above code is compiled and executed, it produces the following result −
Please Enter your Name
John Smith
Please Enter the name of your Organisation
Infotech
Hello John Smith from Infotech

Following example makes use of few more functions, let's see −
program exString;
uses sysutils;
var
   str1, str2, str3 : ansistring;
   str4: string;
   len: integer;

begin
   str1 := 'Hello ';
   str2 := 'There!';
   
   (* copy str1 into str3 *)
   str3 := str1;
   writeln('appendstr( str3, str1) :  ', str3 );
   
   (* concatenates str1 and str2 *)
   appendstr( str1, str2);
   writeln( 'appendstr( str1, str2) ' , str1 );
   str4 := str1 + str2;
   writeln('Now str4 is: ', str4);
   
   (* total lenghth of str4 after concatenation  *)
   len := byte(str4[0]);
   writeln('Length of the final string str4: ', len); 
end.
When the above code is compiled and executed, it produces the following result −
appendstr( str3, str1) : Hello
appendstr( str1, str2) : Hello There!
Now str4 is: Hello There! There!
Length of the final string str4: 18

Pascal supports a wide range of functions and procedures that manipulate strings. These subprograms vary implement-wise. Here, we are listing various string manipulating subprograms provided by Free Pascal −
function AnsiCompareStr(const S1: ; const S2:):Integer; 
Compares two strings
function AnsiCompareText(const S1: ; const S2:):Integer;
Compares two strings, case insensitive
function AnsiExtractQuotedStr(var Src: PChar; Quote: Char):;
Removes quotes from string
function AnsiLastChar(const S:):PChar;
Gets last character of string
function AnsiLowerCase(const s:):
Converts string to all-lowercase
function AnsiQuotedStr(const S: ; Quote: Char):;
Quotes a string
function AnsiStrComp(S1: PChar;S2: PChar):Integer;
Compares strings case-sensitive
function AnsiStrIComp(S1: PChar; S2: PChar):Integer;
Compares strings case-insensitive
function AnsiStrLComp(S1: PChar; S2: PChar; MaxLen: Cardinal):Integer;
Compares L characters of strings case sensitive
function AnsiStrLIComp(S1: PChar; S2: PChar; MaxLen: Cardinal):Integer;
Compares L characters of strings case insensitive
function AnsiStrLastChar(Str: PChar):PChar;
Gets last character of string
function AnsiStrLower(Str: PChar):PChar;
Converts string to all-lowercase
function AnsiStrUpper(Str: PChar):PChar;
Converts string to all-uppercase
function AnsiUpperCase(const s:):;
Converts string to all-uppercase
procedure AppendStr(var Dest: ; const S:);
Appends 2 strings
procedure AssignStr(var P: PString; const S:);
Assigns value of strings on heap
function CompareStr(const S1: ; const S2:):Integer; overload;
Compares two strings case sensitive
function CompareText(const S1: ; const S2:):Integer;
Compares two strings case insensitive
Removes string from heap
procedure DisposeStr(S: PShortString); overload;
Removes string from heap
function IsValidIdent( const Ident:):Boolean;
Is string a valid pascal identifier
function LastDelimiter(const Delimiters: ; const S:):Integer;
Last occurrence of character in a string
function LeftStr(const S: ; Count: Integer):;
Gets first N characters of a string
function LoadStr(Ident: Integer):; 
Loads string from resources
function LowerCase(const s: ):; overload; 
Converts string to all-lowercase
function LowerCase(const V: variant ):; overload; 
Converts string to all-lowercase
function NewStr(const S:):PString; overload;
Allocates new string on heap
function RightStr(const S: ; Count: Integer):;
Gets last N characters of a string
function StrAlloc(Size: Cardinal):PChar;
Allocates memory for string
function StrBufSize(Str: PChar):SizeUInt;
Reserves memory for a string
procedure StrDispose(Str: PChar);
Removes string from heap
function StrPas(Str: PChar):; 
Converts PChar to pascal string
function StrPCopy(Dest: PChar; Source:):PChar;
Copies pascal string
function StrPLCopy(Dest: PChar; Source: ; MaxLen: SizeUInt):PChar;
Copies N bytes of pascal string
function UpperCase(const s:):;
Converts string to all-uppercase

    94 Lectures 
     8.5 hours 

 Stone River ELearning
 Print
 Add Notes
 Bookmark this page