Substring Assignment

A value can be assigned to a substring of a string variable, dynamic array or dimensioned array variable using the three assignment statements described below.

Syntax

X[start#,replace#] = expression

X{<attr#{,value#{,subvalue#}}>}{[start#, replace#]} = expression

X(row{,column}) {<attr#{,value#{,subvalue#}}>} {[start#,replace#]} = expression

Comments

The first form of the statement replaces the substring defined by start# (the first character position) and replace# (the number of characters) with the string given by expression.

The second form replaces a substring of dynamic array X with the string given by expression.

The third form replaces a substring of dimensioned array X with the string given by expression.

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

In the statement

X[start#,replace#] = "ABC"

start# and replace# have the following meanings:

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

start# = 0 Same as the value start# = 1.

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

replace# > 0 The number of characters to replace in the substring in X, starting with start# and continuing left to right.

replace# = 0 A substring of zero length is replaced in X, meaning that the new string ("ABC" in the example) is inserted, starting at character position start#. If start# >= 0, the new string is inserted to the left of start#. If start# < 0, the new string is inserted to the right of start#. X[1,0]  concatenates a string to the front of X and X[-1,0]  concatenates a string to the end of X.

replace# < 0 The last character of the substring, counting from the right side of the string. If replace# is -1, the substring is replaced, from start# to the end of the string, with the expression on the right-hand-side. This makes it easy to manipulate strings of unknown length.

Examples

If X = "ABCDEFGH", the following substring values are assigned:

X[-5,-3]="vwxyz"

X becomes ABCvwxyzGH by replacing the substring starting five characters from the end of string X and ending three characters from the end.

X[1,0]="999"

X becomes 999ABCDEFGH by concatenating the subvalue 999 to the front of string X.

Go to top button