50 STOP

Type NEW when this program has been run and enter the next program:

10 a$="ABLE WAS I"
20 FOR n=1 TO 10
30 PRINT a$(n TO
   10),a$((11-n) TO 10)
40 NEXT n

For string variables, we can not only extract substrings, but also assign to them. For instance, type:

a$="I'm the ZX Spectrum Next"

and then:

a$(5 TO 8)="******"

and:

PRINT a$

Notice how since the substring a$(5 TO 8) is only 4 characters long, only the first four stars have been used. This is a characteristic of assigning to substrings: the substring has to be exactly the same length afterwards as it was before. To make sure this happens, the string that is being assigned to it is cut off on the right if it is too long, or filled out with spaces if it is too short – this is called Procrustean assignment after the road bandit Procrustes who used to make sure that his victims fitted the bed by either stretching them out on a rack or cutting their feet off.

If you now try:

a$()="Hello there"

and:

a$;"."

You will see that the same thing has happened again (this time with spaces put in) because a$() counts as a substring.

a$="Hello there"

will do it properly.

Complicated string expressions will need parentheses around them before they can be sliced. For example:

"abc"+"def"(1 TO 2)="abcde"

("abc"+"def")(1 TO 2)="ab"

String multiplication using the * operator

We saw earlier that the multiply (*) operator can be used for string replication. It's syntax is: a$*n (takes a string a$ as the first operand and a number n as the second operand, returning a string). If n has a fractional part then the string is replicated up to the character that represents the closest integer number to the product of n times the length of the original string. In other words, if you multiply a 7 character string by 1.5 times you will get the whole string and an additional 3 characters of it since 7 * 1.5 = 10.5. Finally, the sign of the number determines whether the result is mirrored or not. Consider the following examples:


ZX Spectrum Next User Manual, 3rd Edition (ISBN 978-1-5272-5496-1), written and illustrated by Phoebus R. Dokos. Copyright © 2020-2024 Phoebus Dokos / SpecNext Ltd. Licensed under CC BY-NC-SA 4.0. This is a transcription and can contain errors; check any doubt against the printed page.