Pascal - Strings
- Details
- Category: Chapter 3
- Published: Sunday, 14 April 2013 16:21
- Written by Sternas Stefanos
- Hits: 20582
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 commonly 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.
- String variables: The variable of String type, as defined in Turbo Pascal.
- Short strings: The variable of String type with size specification.
- Null terminated strings: The variable of pchar type.
- AnsiStrings: Ansistrings are strings that have no length limit.
Pascal provides only one string operator . string concatenation operator (+).
1. Examples
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 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 following result:
appendstr( str3, str1) : Hello appendstr( str1, str2) : Hello There! Now str4 is: Hello There! There! Length of the final string str4: 18
2. Pascal String Functions and Procedures
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:
S.N. | Function & Purpose |
---|---|
1 | function AnsiCompareStr( const S1: ; const S2: ):Integer; Compare two strings |
2 | function AnsiCompareText( const S1: ; const S2: ):Integer; Compare two strings, case insensitive |
3 | function AnsiExtractQuotedStr( var Src: PChar; Quote: Char ):; Removes quotes from string |
4 | function AnsiLastChar( const S: ):PChar; Get last character of string |
5 | function AnsiLowerCase( const s: ): Convert string to all-lowercase |
6 | function AnsiQuotedStr( const S: ; Quote: Char ):; Qoutes a string |
7 | function AnsiStrComp( S1: PChar; S2: PChar ):Integer; Compare strings case-sensitive |
8 | function AnsiStrIComp( S1: PChar; S2: PChar ):Integer; Compare strings case-insensitive |
9 | function AnsiStrLComp( S1: PChar; S2: PChar; MaxLen: Cardinal ):Integer; Compare L characters of strings case sensitive |
10 | function AnsiStrLIComp( S1: PChar; S2: PChar; MaxLen: Cardinal ):Integer; Compare L characters of strings case insensitive |
11 | function AnsiStrLastChar( Str: PChar ):PChar; Get last character of string |
12 | function AnsiStrLower( Str: PChar ):PChar; Convert string to all-lowercase |
13 | function AnsiStrUpper( Str: PChar ):PChar; Convert string to all-uppercase |
14 | function AnsiUpperCase( const s: ):; Convert string to all-uppercase |
15 | procedure AppendStr( var Dest: ; const S: ); Append 2 strings |
16 | procedure AssignStr( var P: PString; const S: ); Assign value of strings on heap |
17 | function CompareStr( const S1: ; const S2: ):Integer; overload; Compare two strings case sensitive |
18 | function CompareText( const S1: ; const S2: ):Integer; Compare two strings case insensitive |
19 | procedure DisposeStr( S: PString ); overload; Remove string from heap |
20 | procedure DisposeStr( S: PShortString ); overload; Remove string from heap |
21 | function IsValidIdent( const Ident: ):Boolean; Is string a valid pascal identifier |
22 | function LastDelimiter( const Delimiters: ; const S: ):Integer; Last occurance of character in a string |
23 | function LeftStr( const S: ; Count: Integer ):; Get first N characters of a string |
24 | function LoadStr( Ident: Integer ):; Load string from resources |
25 | function LowerCase( const s: ):; overload; Convert string to all-lowercase |
26 | function LowerCase( const V: variant ):; overload; Convert string to all-lowercase |
27 | function NewStr( const S: ):PString; overload; Allocate new string on heap |
28 | function RightStr( const S: ; Count: Integer ):; Get last N characters of a string |
29 | function StrAlloc( Size: Cardinal ):PChar; Allocate memory for string |
30 | function StrBufSize( Str: PChar ):SizeUInt; Reserve memory for a string |
31 | procedure StrDispose( Str: PChar ); Remove string from heap |
32 | function StrPas( Str: PChar ):; Convert PChar to pascal string |
33 | function StrPCopy( Dest: PChar; Source: ):PChar; Copy pascal string |
34 | function StrPLCopy( Dest: PChar; Source: ; MaxLen: SizeUInt ):PChar; Copy N bytes of pascal string |
35 | function UpperCase( const s: ):; Convert string to all-uppercase |