Strings
 
Syntax


DIM symbolname$ ( maxlength )     ' kept for backward compatibility

   or

DIM symbolname ( maxlength ) AS STRING 

  or

DIM symbolname ( maxlength ) AS BYTE

Description


A STRING is a special array of BYTE terminated by a byte of 0, and is limited to 256 characters.  String operations must be limited to the first 256 characters and there is no runtime check.

Despite the use of the maxlength , an implicit &H0 is added to the end of the STRING, to allow for variable length during program execution.  For this reason a &H0 may not be used within a string. 

Byte arrays can be used using an allocation as BYTE, and they may exceed 256 characters.  They may also contain embedded &H0 elements.  But if they do, string operations can not be used.  For instance a byte array of &H0, &H1, &H2 can be built as-

    astr(0) = 0
    astr(1) = 1
    astr(2) = 2
    astr(3) = 3

But the following will NOT work-

    astr = chr(0) + chr(1) + chr(2) + chr(3)   ' fails as the first 0 terminates this string operation

But it can be done as-

    astr = chr(1) + chr(1) + chr(2) + chr(3)
    astr(0) = 0                                          ' replace the first character with a $0 

STRINGs are not checked for length at run time, so care must be taken to avoid filling it beyond the declared DIM.

Individual characters within a string can be accessed like an array, such as astr(12) returns the character in position 13, with the first element at offset 0.

Single character strings are a special case, and usually replaced by the byte constant representing that character.  So "A" can be used interchangeably with &H41 or 63.

Example

'  Fixed-length declaration, but value varies during execution
DIM astr (20) as STRING
astr = "Hello"

astr = astr+chr(32)+ "World"


PRINT astr        '   = "Hello World"

Differences from other BASICs

See also