LEN "Fred"+ LEN "Bloggs"LEN ("Fred"+"Bloggs")
4+LEN "Bloggs"LEN ("FredBloggs")
4+6LEN "FredBloggs"
1010

Functions can be separated in built-in (contained within NextBASIC) and user-defined ones (the ones we write). Below we'll visit the in-built functions categorised according to the datatype we give them as input to manipulate and/or transform.

Please note that as the functions devoted to maths are a bit more complicated we will list them separately in their own chapter.

String functions

LEN

LEN x$, works out the length of a string. Its single argument x$ is the string whose length you want to find, and its result is the length, so that if you type

PRINT LEN "ZX Spectrum Next"

the answer 16 will be printed on screen, that is the number of characters in ZX Spectrum Next (spaces are counted as a character).

STR$

STR$ converts numbers into strings; that however is not its only capability and for that reason it has two forms. Apart from the simplest task of making a string out of a number in the way it would appear on screen displayed by a PRINT statement (that also means converted – or cast – into a decimal number), it can also do the same but converting at the same time the number to a different base!

The simplest form STR$ x doen't use parentheses to contain its single argument x –a number–, and its result is the string that would appear on the screen if the number were displayed by a PRINT statement. Note how its name ends in a $ sign to show that its result is a string. For example, you could say:

a$=STR$ 1e2

which would have exactly the same effect as typing:

a$="100"

since the numeric argument gets converted from scientific notation, to a standard decimal prior to printing. Or you could say:

PRINT LEN STR$ 100.000

and get the answer 3, because STR$ 100.0000="100".

STR$(x, [base [, places]]) on the other hand uses parentheses, but takes up to 3 numeric arguments. It returns a string representation of number n in the optionally specified base (2 to 36). For bases > 10, digits larger than 9 are represented with capital letters starting with A. If the base is not specified it's assumed it's 10. If optional argument places is present, then a fractional part of places' digits is also output. Let's first rewrite the example above in a couple of ways:

a$=STR$ (1e2)
a$=STR$ (1e2,16)

If you assumed prior to trying, that the first produces the same result as the STR$ without parentheses further above, you'd be correct. It too, returns 100. The second however demonstrates the power of conversion as we told STR$ to convert to base-16 (hexadecimal). This returns 64 which is 100 expressed in the hexadecimal system. The following two


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.