[ ] operator

Syntax

expression [ [ start, ] length ]
expression [ delimiter, occurrence, fields ]

Description

Use the [ ] operator (square brackets) to extract a substring from a character string. The bold brackets are part of the syntax and must be typed.

expression evaluates to any character string.

start is an expression that evaluates to the starting character position of the substring. If start is 0 or a negative number, the starting position is assumed to be 1. If you omit start, the starting position is calculated according to the following formula:

string.length - substring.length + 1

This lets you specify a substring consisting of the last n characters of a string without having to calculate the string length.

If start exceeds the number of characters in expression, an empty string results. An empty string also results if length is 0 or a negative number. If the sum of start and length exceeds the number of characters in the string, the substring ends with the last character of the string.

length is an expression that evaluates to the length of the substring.

Use the second syntax to return a substring located between the specified number of occurrences of the specified delimiter. This syntax performs the same function as the FIELD function.

delimiter can be any string, including field mark, value mark, and subvalue mark characters. It delimits the start and end of the substring (all that appears within the two delimiters). If delimiter consists of more than one character, only the first character is used.

occurrence specifies which occurrence of the delimiter is to be used as a terminator. If occurrence is less than 1, 1 is assumed.

fields specifies the number of successive fields after the delimiter specified by occurrence that are to be returned with the substring. If the value of fields is less than 1, 1 is assumed. The delimiter is part of the returned value in the successive fields.

If the delimiter or the occurrence specified does not exist within the string, an empty string is returned. If occurrence specifies 1 and no delimiter is found, the entire string is returned.

If expression is the null value, any substring extracted from it will also be the null value.

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"
PRINT A["#",4,1]

This is the result:

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]

This is the result:

67890

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

A='12345'
A[3]=1212
PRINT "A=",A

returns the following:

A=   121212

A[3] replaces the last three characters of A (345) with the newly assigned value for that substring (1212).

The FIELDSTORE function provides the same functionality as assigning the three-argument syntax of the [ ] operator.