[comp.lang.pascal] Extract single char from a Str.

ZCCBJSB%EB0UB011.BITNET@cunyvm.cuny.edu (Josep Sau B.) (04/19/91)

Hi, Tim Clinkenpeel.

 The result of a STRING FUNCTION, like Copy, will be always
a STRING not a CHAR. Notice the difference in memo.alocation:

   a CHAR VAR = 1 byte in memory.

   a STRING VAR = 1 length byte + array of char

So Copy(Str,i,1) returns another string of length 1
(at least 2 bytes in memory), not one single char (1 byte).

To extract one single character from a STRING and assign that
value to a CHAR VAR you have to use the ARRAY_OF_CHAR-like
notation:

 ch := str1!;  (* assign ch :CHAR the first CHAR in Str :STRING *)

This may be dangerous, though, if you do not check first if
that character is in the valid range according to the current
string length.

The book you spoke about is right, but that paragraph lacks
part of needed information (or you did not read overleaf :-)),
"single character extractions from strings of characters"
must mean the method stated above...

As far as I know this approachment to strings as a static
array of char with a dynamic length byte has been implemented
in Turbo since version 3.0 (Am I right?).

Anyway, I like it very much because it gives the programmer
'friendly' variable length string handling, but at the same
time very fast raw access to string character components.

-- Josep Sau


'...and he amazed with the marvels that are in the world,
    and he asked about things he did not understand,
    and he taught about things he knew.'
 Raimundus Lulius - _Llibre_de_Meravelles_