Please enable JavaScript to view this site.

A-Shell Reference

Navigation: A-Shell BASIC (ASB) > Operators

Substring Operator

Scroll Prev Top Next More

(Also known as the "Slice" Operator.) In addition to more traditional BASIC substring functions LEFT$(), MID$(), and RIGHT$(), ASB also supports an array-like syntax for directly referencing substrings (aka string slices), both in general source expressions, as well as for making assignments directly to a substring. There are two variations:

var[spos,epos]

var[spos;length]

The parameters spos (starting position), epos (ending position), and length may be positive or negative. When positive, they are relative to the left side of the string (first position is 1). When negative, they are relative to the right side of the string (right-most position is -1). In the case of length, a negative value adjusts the ending position to the left, rather than the right.

For example:

map1 var,s,16

var = "1234567890"

 

? var[1,3]             ! "123"

? var[5;3]             ! "567"

? var[-3,-1]           ! "890"

? var[5,-3]            ! "345"

? var[4,-4]            ! "4567"

 

When the edge of the slice goes outside the boundaries of the string, it is effectively shifted over to the nearest edge. So var[0,3] is equivalent to var[1,3], but note that var[0;3] is equivalent to var[1;2], since the ending position is calculated from the requested starting position and length, before the adjustment is made.

Note that in the above examples, when negative spos and epos values were taken to be relative to the logical end of the string. But when the substring operator appears on the left side of an assignment operator, the rule changes and the indices are taken to be relative to the physical string. Continuing the example above with the same var mapped as a 16 byte string containing "01234567890"...

var[5;3] = "ABC"  : ? var        ! "01234ABC890"

var[-3,-1] = "XYZ" : ? var       ! "01234ABC890   XYZ"

 

The result of the second statement above often comes as a surprise, since it is asymmetric with the way var[-3,-1] is interpreted when on the right of an assignment. But as previously noted, when making an assignment directly to a substring, the indices operate on the physical rather than logical string variable. Furthermore, in the case of an S (rather than X) variable, any positions between the previous logical end of the string and the beginning of the assigned area will be filled in with spaces. Otherwise, the XYZ in the above example would not appear in the print statement because they would be beyond the first null terminator. In the case of X variables, there is no special filling of the intermediate positions.

When assigning to a substring beyond the current length of a dynamic variable, the behavior depends on the A-Shell version. See History notes below.

See Also

History

2018 June, A-Shell 6.5.1639.0:  Assignments to substrings (slices) of dynamic variables now automatically extend the target variable as needed to contain the slice. Previously, the target variable was not extended, acting instead more like a fixed length variable in that case.