Chapter 21 - Substrings
 
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 STRING" & "BIG REG" are not.
 
    There is a notation called slicing
 for describing substrings, & 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 for misses out the 
TO
 & just has one number:
 
        "ABCDEF" (3) = "ABCDEF" (3 
TO
 3) = "C"
 
    Although normally both start & finish must refer to existing parts of the string, this rule is overridden by 
one other: if the start is more than the finish, then the result is the empty string. So
 
        "ABCDEF" (5 
TO
 7)
 
gives error 3 (subscript error) because, the string only contains 6 characters, & 7 is too many, but
 
        "ABCDEF" (8 
TO
 7) = ""
 
&
 
        "ABCDEF" (1 
TO
 0) = ""
 
    The start & finish must not be negative, or you get error B.
 
    This next program makes B$ equal to A$, but imitting any trailing spaces.
 
        10 
INPUT
 A$
 
        20 
FOR
 N=
LEN
 A$ 
TO
 1 
STEP
 -1
 
        30 
IF
 A$(N)<>"" 
THEN GOTO
 50
 
        40 
NEXT
 N
 
        50 
LET
 B$=A$( 
TO
 N)
 
        60 
PRINT
 "
""
";A$;"
""
","
""
";B$;"
""
"
 
        70 
GOTO
 10
 
    Note how if A$ is entirely spaces, then in line 50 we have N = 0 & A$( 
TO
 N) = A$(1 
TO
 0) = "".
 
    For string variables, we can not only extract substrings, but also assign to them. For instance type
 
 
LET
 A$="LOR LOVE A DUCK"
 
& then
 
 
LET
 A$(5 TO 8)="******"
 
&
 
 
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