Chapter 7 – Strings

Introduction

As we discussed previously strings are series of characters stored as numbers in memory. Which number represents which character is usually governed by a standard. For NextBASIC this standard is a modified version of the ASCII standard that apart from characters, also defines tokens and UDGs. See Chapters 13 and 23 as well as Appendix A in order to better understand how characters, tokens and UDGs are stored. This chapter deals with ways of manipulating strings and reiterates some functions touched upon by previous chapters.

String slicing, using TO

Given a string, a substring of it consists of some consecutive characters from it, taken in sequence. Thus “string” is a substring of “bigger string”, but “b sting” and “big reg” are not.

There is a notation called slicing for describing substrings, and this can be applied to arbitrary string expressions. The general form is:

string expression (start TO finish)

so that, for instance:

"abcdef"(2 TO 5)="bcde"

If you omit the start, then 1 is assumed; if you omit the finish then the length of the string is assumed. Thus:

"abcdef"( TO 5)="abcdef"(1 TO 5)="abcde"

"abcdef"(2 TO )="abcdef"(2 TO 6)="bcdef"

"abcdef"( TO )="abcdef"(1 TO 6)="abcdef"

(You can also write this last one as "abcdef"(), for what it's worth.)

A slightly different form misses out the TO and just has one number:

"abcdef"(3)="abcdef"(3 TO 3)="c"

Although normally both start and finish must refer to existing parts of the string, this rule is overridden by another one: if the start is more than the finish, then the result is the empty string. So:

"abcdef"(5 TO 7)

gives error 3 Subscript wrong because the string only contains 6 characters and 7 is too many, but:

"abcdef"(8 TO 7)="" (an empty string)

and:

"abcdef"(1 TO 0)="" (again, an empty string)

The start and finish must not be negative, or you get error B integer out of range. This next program is a simple one illustrating some of these rules.

10 a$="abcdef"
20 FOR n=1 TO 6
30   PRINT a$(n TO 6)
40 NEXT n

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.