Next: , Previous: Pointer Arithmetics, Up: Programming

6.5 Accessing parts of strings (and other arrays)

GPC allows the access of parts (“slices”) of strings as defined in Extended Pascal. For example:

     program StringSliceDemo;
       HelloWorld = 'Hello, world!';
       WriteLn (HelloWorld[8 .. 12])  { yields `world' }

As an extension, it also allows write access to a string slice:

     program SliceWriteDemo;
       s: String (42) = 'Hello, world!';
       s[8 .. 12] := 'folks';
       WriteLn (s)  { yields `Hello, folks!' }

As a further extension, GPC allows slice access also to non-string arrays. However, the usefulness of this feature is rather limited because of Pascal's strict type checking rules: If you have, e.g., an array [1 .. 10] of Integer and take a slice [1 .. 5] of it, it will not be compatible to another array [1 .. 5] of Integer because distinct array types are not compatible in Pascal, even if they look the same.

However, array slice access can be used in connection with conformant or open array parameters. See the program arrayslicedemo.pas (in the demos directory) for an example.