CHAPTER
11
HANDLING
TEXT
STRINGS
You
have used string variables to store character strings and you
know
that the rules
for manipulating string variables or string constants are not the same
as
those for numeric
variables or numeric constants SuperBASIC offers a full range of facilities for manipulating
character strings effectively.
In
particular the
concept
of string-slicing both extends
and
simplifies the business of handling substrings or slices of a string.
•
ASSIGNING
STRINGS
Storage for string variables
is
allocated as
it
is
required by a program. For example,
the lines: .
100
LET
words$
=
"LONG"
110
LET
words$
="LONGER"
120
PRINT
words$
would cause the six letter word, LONGER,
to
be
printed. The first line would cause
space
for four letters to be allocated
but
this allocation would
be
overruled by the
second
line
which
requires
space
for six characters.
It
is,
however, possible to dimension
(i.e.
reserve
space
for) a string variable, in which
case the
maximum
length
becomes
defined, and the variable behaves as an array.
JOINING
STRINGS
You
may wish
to
construct records
in
data processing from a number of sources. Suppose,
for example, that you are a teacher
and
you want
to
store a set of three
marks
for each
student in Literature, History, and Geography. The marks are held in variables as shown:
•
lil$0
hist$0
geog$
0
As
part of student record keeping you
may
wish to
combine
the three string values Into
one
six-character string called mark$.
You
simply write:
LET
mark$
= l i
t$
&
hi
st$
&
geog$
You
have created a further variable as shown:
mark$!625671
I
But
remember
that you are dealing with a character string
which
happens
to contain
number
characters rather than an actual number. Note that in SuperBASIC the & symbol
is
used
to
jOin
strings together, whereas
In
some other BASICs, the + symbol
IS
used
for that pu
rpose
•
sIring slice
A string slice is part of a string.
It
may
be anything from a single character to the
whole
string.
In
order
to identify the string slice you
need
to know the positions of the required
characters.
Suppose
you are constructing a children\>
game
in which they have to recognise a word
hidden
in a
jumble
of letters. Each letter has an internal
number
- an index -
corresponding to
its
position
in
the string. Suppose the whole string
is
stored
in
the variable
iumble$,
and
the clue
is
Big
cat
COpy
A
STRING
SLICE
jumble$
A P
Q
a L L I a N
A T
S
U
Z
•
58
2 3 4 5 6 7 8 9
10
11
12
13 14
12/84