12.2 Using Dynamic Strings
Although dynamic strings are less efficient than fixed-length
strings, they are often more flexible. For example, to con-
catenate strings, you can use the LET statement to assign
the concatenated value to a dynamic string variable, without
having to be concerned about BASIC truncating the string
or adding trailing spaces to it. However, if the destination
variable is fixed-length, you must make sure that it is long
enough to receive the concatenated string, or BASIC trun-
cates the new value to fit the destination string. Similarly, if
you use LSET or RSET to concatenate strings, you must en-
sure that the destination variable is long enough to receive
the data.
The LET, LSET, and RSET statements all operate on dy-
namic strings as well as fixed-length strings. The LET
statement can change the length of a dynamic string; LSET
and RSET do not. LSET and RSET are more efficient than
LET when changing the value of a dynamic string. For more
information about LSET and RSET, see Sections 12.5.2 and
12.5.3.
In the following example, the first line assigns the value
``ABC'' to A$ , the second line assigns ``XYZ'' to B$ , and the
third line assigns six spaces to C$ . These variables are dy-
namic strings. In the fourth line, LSET assigns A$ the value
of A$ concatenated with B$ . Because the LSET statement
does not change the length of the destination string vari-
able, only the first three characters of the expression A$ + B$
are assigned to A$ . The fifth line uses LSET to assign C$ the
value of A$ concatenated with B$ . Because C$ already has a
length of 6, this statement assigns the value ``ABCXYZ'' to it.
LET A$ = "ABC"
LET B$ = "XYZ"
LET C$ = " "
LSET A$ = A$ + B$
LSET C$ = A$ + B$
PRINT A$
PRINT C$
END
Output
ABC
ABCXYZ
Like the LET statement, the INPUT, INPUT LINE, and
LINPUT statements can change the length of a dynamic
string, but they cannot change the length of a fixed-length
string.
In this example, the first line assigns the null string to vari-
able A$ . The second line uses the LEN function to show
that the null string has a length of zero. The third line uses
the INPUT statement to assign a new value to A$ , and the
fourth and fifth lines print the new value and its length.
!Declare a dynamic string
LET A$ = ""
PRINT LEN(A$)
INPUT A$
PRINT A$
PRINT LEN(A$)
END
Output
0
? THIS IS A TEST
THIS IS A TEST
14
You should not confuse the null string with a null character.
A null character is one whose ASCII numeric code is zero.
The null string is a string whose length is zero.