TO
String Slicer
TO has two different uses in BASIC. It is used in conjunction with FOR to set up a FOR-NEXT loop (see FOR for further details) and it is also used in string slicing (the splitting up of strings into smaller sub-strings).
How to use TO for string slicing
TO is used to define the first and last characters of a sub-string within a main string. TO is preceded by a string value, an opening bracket, then an optional numeric value. It is followed by an optional numeric value and then a closing bracket, for example
80 PRINT a$(4 TO 7)
A string expression must also be enclosed in brackets. The string value (a$ above) is the string that is to be sliced. The two numeric values (4 and 7) define the positions of the first and last characters of the sub-string within the string. TO then returns the sub-string (characters 4 to 7 inclusive, of a$).
The first numeric value has a default value of 1 and the second has a default value equal to the position of the last character in the string. The first value can therefore be omitted if the sub-string begins with the first character in the string, and the second value can be omitted if the sub-string ends with the last character in the string.
Format
- string-const ([num-expr] TO [num-expr])
- string-var ([num-expr] TO [num-expr])
- (string-expr) ([num-expr] TO [num-expr])
See also