Substring Extraction

A substring value can be extracted from a string, dynamic array or dimensioned array using the extraction statements described below.

Syntax

X = string[start#,length]

X = dyn-array{<attr#{,value#{,subvalue#}}>} {[start#,length]}

X = dim-array(row{,column}) {<attr#{,value# {,subvalue#}}>} {[start#,length]}

The first form of the statement extracts a substring from a string, where start# is the starting character position and length is the length of the substring to be extracted. Refer to the sub-section Substring Expression.

The second form extracts a substring from a dynamic array.

The third form extracts a substring from a dimensioned array.

Note: The discussion and examples in this section concentrate on simple substring extraction. For more information about array extraction, refer to the description of arrays.

In the statement:

X = VAR[start#,length]

start# and length have the following meanings:

start# > 0 The starting position of the substring, from the left.

start# = 0 Same as the value start# = 1. See the second example.

start# < 0 The starting position of the substring counting backward from the right. For example, -1 is the last character of the string, -2 is second to the last, and so on.

length > 0 The number of characters, starting from start# to extract. Characters are extracted from left to right.

length = 0 Returns a null string.

length < 0 The number of the character, counting from the ending position, at which to stop substring extraction. If you specify -1 as the length of the substring, the substring starting at position start# is extracted. This makes it easy to manipulate strings of unknown length.

Examples

If VAR = "12345678", the following substrings are extracted:

X=VAR[3,2]  evaluates to 34, by starting at the third character position from the beginning of string VAR and extracting a substring length 2.

X=VAR[0,2]  evaluates to 12, by starting at the first character position 1 from the beginning of VAR and extracting a substring length 2. Same as X=VAR[1,2].

X=VAR[-3,2]  evaluates to 67, by starting at the third character position from the end of VAR and extracting a substring length 2.

X=VAR[4,-3]  evaluates to 456, by starting at the fourth character position from the beginning of VAR and extracting a substring up to and including the third character from the end.

Go to top button