Strings (Advanced)

Top  Previous  Next

Another way to set up a character array is to make a text string. For example:

 

        "This is a string"

 

This allocates some memory space, fills it with the ASCII for each character, and returns the starting address. If this address is assigned to the character variable S then S is like any other character array except that the contents are already set.

 

We can read the individual bytes, as in:

 

        character  S;

        begin

        S:= "This is a string";

        if S(3)=$73 then Text(0, "It's an s");

        . . .

 

Or we can store bytes into this array, as in:

 

      S(3):= ^n;   S(5):= ^a;

 

We can output the string to any device using the Text intrinsic. For example:

 

        Text(0, S);

 

now displays:

 

        Thin as a string

 

on the monitor (device 0).

 

Note that the quoted string itself allocates the memory space; there is no dimension after the S in the declaration. Writing: "character S(16);" would allocate another 16 bytes that would not be used.

 

The end of a string is marked by setting the high bit of the last character. This adds $80 (128) to the ASCII value of this character. In the example above, S(15) has the value $E7, which is $80 more than the ASCII for the letter g ($67).

 

The caret character (^), besides indicating ASCII values (see: ASCII Constants), enables quotes (") and carets to be in strings. For example:

 

        Text(0, "^"^^^" is called a ^"caret^"");

 

displays:

 

        "^" is called a "caret"

 

A string can contain any printable character. It can also contain control characters like tab, carriage return, bell, and form feed. However, putting a form feed in a string can mess up a program listing, and a control character, such as a bell ($07), does not show in the listing. For these reasons, it is better to use the caret character to put a control character in a string.

 

Inside a string, ^A means control-A, ^Z means control-Z, and so forth. Do not confuse this use of the caret character with the way it is used to represent an ASCII character outside a string. ^G in a string means control-G ($07, the bell character), but outside a string it means the letter G ($47).

 

Characters in addition to A-Z can be used with the caret to get the complete range of control characters. The symbols ^@, ^A...^Z, ^[, ^\, ^], and ^_ correspond to the values $00, $01...$1A, $1B, $1C, $1D, and $1F. Note the exception: ^^, which is not $1E but the caret character ($5E) described above. Lowercase letters and characters can also be used. ^`, ^a...^z, ^{, ^|, ^}, and ^~ correspond to the values $00, $01...$1A, $1B, $1C, $1D, and $1E.