string_variable[start_index:string_length]
A substring expression lets you extract a part (substring) of a string. The start_index gives the starting character position of the substring within the string, and string_length gives the number of characters to include in the substring, including the starting character. If string_length is not given, then it is assumed to be to the end of the originating string. A negative start_index leads to the basing of the substring from the end of the string.
Suppose the variable STR has the value "ABCDEF":
STR[1] "ABCDEF"
STR[3:1] "C"
STR[3] "CDEF"
STR[2:3] "BCD"
STR[5] "EF"
STR[4:7] "DEF"
STR[-2] "EF"
STR[-4:2] "CD"
Both start_index and string_length can be numeric expressions as well as constants. For example, to obtain the last 3 characters of STR you could use the expression:
In this example, if STR is not at least two characters long, you may get unexpected results. You could also write the above as:
Likewise, substring expressions can be performed on string arrays. Suppose the string array crop had the following definition:
The following substring expressions would yield the results as shown:
crop(1)[2] "aize"
crop(1)[3:1] "i"
crop(2)[3] "eat"
crop(3)[2] "ice"
crop(4)[5] "toes"
crop(5)[1:3] "leg"