Back to DFS's Pascal Page


Strings in Turbo Pascal

Follow these fast links:

String Basics in Turbo Pascal

In Turbo Pascal, a string is stored as a char array. The zeroth location in the array contains a binary value equal to the usable length of the string, i.e., no characters past the subscript equal to this value are treated as being part of the string.

Strings may be declared in two ways:

The first method results in a default maximum length of 255, the largest unsigned binary number which can be stored in a byte. This is also the absolute maximum length for a string in Turbo Pascal. This method of string storage has ramifications for how text must be handled.

The following declares a string called mystr. Pascal provides an array consisting of sixteen consecutive bytes of memory. The subscripts used for the array start with zero (0).

mystr : string[15];

The following is a representation of what would be in RAM, if the string 'Hello, world!' were stored in this variable.

Characters: CR  H  e  l  l  o  ,     w  o  r  l  d  ! ?? ??
Hex values: 0D 48 65 6C 6C 6F 2C 20 77 6F 71 6C 64 21 ?? ??
Subscripts:  0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15

The zeroth location contains the current length of the string. As it happens, this value corresponds to the Carriage Return code of the ASCII character set. The last two locations are here filled with the dummy placeholder ??. Anything could be there, but the values are irrelevant because Pascal uses only the 13 characters as specified by the zeroth location.

Back to Top

Concat Function

Syntax: NewString := Concat(str1, str2, ...)
    where NewString, str1, str2, etc. are string identifiers

Concat is used to concatenate all of its parameters into a single string which is then returned. This is equivalent to pasting one string onto the end of another.

There are two common uses for Concat.

Back to Top

Copy Function

Syntax: substr := Copy(mystr, n1, n2)
    where substr and mystr are string identifiers;
        n1 is the starting location for copying; and
        n2 is how many characters are to be copied

Copy is used to copy a portion (or all) of a string into another string variable. Given the following declarations, several things are possible.

S : string[25];
D : string[25];

Back to Top

Delete Procedure

Syntax: Delete(mystr, nStart, nHowmany)
    where mystr is a string identifier; and
    nStart and nHowmany are integers

Delete eliminates nHowmany characters from mystr starting with the nStartth character.

After the following code fragment is executed, mystr will be 'cool'.

mystr := 'school';
Delete(mystr, 3, 1);
Delete(mystr, 1, 1)

Back to Top

Insert Procedure

Syntax: Insert(newstr, mystr, nLocation)
    where newstr is a string identifier or constant;
    mystr is a string identifier; and
    nLocation is an integer

Insert puts newstr into mystr starting at nLocation.

After the following code fragment is executed, mystr will be 'Macdonald'.

mystr := 'McDonald';
Delete(mystr, 2, 2);
Insert('acd', mystr, 2)

Back to Top

Length Function

Syntax: len = Length(mystr)
    where len is an integer and
    mystr is the identifier of a string variable

Length returns the length of a string by checking the zeroth location in the array. The following code will result in len having the value 13.

len : integer;
mystr : string[15];

mystr := 'Hello, world!';
len := Length(mystr);

Back to Top

Pos Function

Syntax: Location := Pos(substr, mystr)
    where substr is the substring being looked for and
    mystr is the string being searched

Pos returns as an integer the location (subscript) of where substr was found to start in mystr.

Substr : string;
MyStr : string;
Location : integer;

SubStr := 'don';
MyStr := 'Macdonald';
Location := Pos(SubStr, MyStr)

As a result of the above code, Location will hold the value 4.

Back to Top

Str Procedure

Syntax: Str(number, mystr)
    where number is a numeric data type and
    mystr is a string identifier

Str is used to convert an integer, real, etc. into a string. This procedure permits the programmer to then use the Length function to find out how much space (i.e., characters) would be required to print the item. In this way, you could determine the longest item in a column so you could line all of them up neatly in the column. Given the following declarations and assignments, several things are possible.

num_int : integer;
num_real : real;
mystr : string;

num_int := 345;
num_real := 34.567;

Back to Top

Upcase Function

Syntax: NewChar := Upcase(OldChar)
    where NewChar and OldChar are of type char

Upcase takes the char OldChar and returns the uppercase version of it. Consequently, if the character stored in OldChar is not a lowercase alphabetic, no change occurs and the original character is returned.

This function is very useful when handling user input. Checking of alphabetic input need only be a comparison with an uppercase letter, e.g.,

YesNo : char;

write('Do you wish to continue? (Y/N) ');
readln(YesNo);
if Upcase(YesNo) = 'N' then halt

Individual characters in a string can also be changed using this function, e.g., After the following code fragment is executed, mystr will be 'McDonald'.

mystr := 'Mcdonald';
mystr[3] := Upcase(mystr[3])

Back to Top

Val Procedure

Syntax: Val(mystr, number, errorflag)
    where mystr is a string identifier;
    number is a numeric data type; and
    errorflag is an integer

Val attempts to convert the string mystr into its numeric representation of the same type as that of number. If the attempt fails because an character illegal for the given data type is found, its position in the string is placed in errorflag. A value of 0 indicates that the conversion was successful. This procedure is extremely useful in checking user input so as to avoid run-time errors when the user mistypes. Examine the following examples and the bulletproofing code in Getting Good Data with the repeat-until Loop II.

num_int : integer;
num_real : real;
mystr : string;
errorflag : integer;

Back to Top

Miscellaneous Notes

Single characters can be replaced in a string. Given the following declarations and assignments, several things are possible.

mystr : string;
ch : char;
D : string[25];

mystr := 'cot;
ch := 'u';
D := 'pat';
  1. Replacing a single character using a char variable:
    D[2] := ch; 
    
    This would result in D containing 'put'.

  2. Replacing a single character using a char literal:
    D[2] := 'e'; 
    
    This would result in D containing 'pet'.

  3. Replacing a single character using a single character from a string variable:
    D[2] := mystr[2]; 
    
    This would result in D containing 'pot'.

Back to Top


© 1999-2007 DFStermole
Created 31 Dec 1999
Last Modified 26 Feb 2007