[] Operator

Syntax

Extracts a substring from a character string. The second syntax acts like the Field function. The square brackets of the [ ] operator are shown in bold italics in the syntax and must be entered.

string [ [ start,] length ]
string[ delimiter, instance, repeats ]

string is the character string. If string is a null value, the extracted value is also null.

start is a number that defines the starting position of the first character in the substring. A value of 0 or a negative number is assumed to be 1. If you specify a starting position after the end of string, an empty string is returned.

length is the number of characters in the substring. If you specify 0 or a negative number, an empty string is returned. If you specify more characters than there are left between start and the end of string, the value returned contains only the number of characters left in string.

delimiter is a character that delimits the start and end of the substring. If delimiter is not found in string, an empty string is returned unless instance is 1, in which case string is returned.

instance specifies which instance of the delimiter marks the end of the substring. A value of less than 1 is assumed to be 1.

repeat specifies the number of times the extraction is repeated on the string. A value of less than 1 is assumed to be 1. The delimiter is returned along with the successive substrings.

Remarks

You can specify a substring consisting of the last n characters of a string by using the first syntax and omitting start.

Examples

In the following example (using the second syntax) the fourth # is the terminator of the substring to be extracted, and one field is extracted:

A = "###DHHH#KK"
B = A["#",4,1]

The result is B equals DHHH.

The following syntaxes specify substrings that start at character position 1:

expression [ 0, length ] 
expression [ -1, length ]

The following example specifies a substring of the last five characters:

"1234567890" [5]

The result is 67890.

All substring syntaxes can be used with the assignment operator ( = ). The new value assigned to the variable replaces the substring specified by the [ ] operator. This usage is not available in expressions. For example:

A = '12345'
A[3] = 1212

The result is A equals 121212.

Because no length argument was specified, A[3] replaces the last three characters of A, (345) with the newly assigned value for that substring (1212).