Substring Operator

A substring is a subset of contiguous characters of a character string. For example, JAMES is a substring of the string JAMES JONES. JAMES JON is also a substring of JAMES JONES.

You can specify a substring as a variable name or an array element specifier, followed by two values separated by a comma and enclosed in square brackets. The two values specify the starting character position and the length of the substring. The syntax is:

expression [ [ start, ] length ]

The bold brackets are part of the syntax and must be typed.

If start is 0 or a negative number, the starting position is assumed to be 1. If start is omitted, 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. So the following substring specification:

"1234567890" [5]

returns the substring:

67890

The following example:

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

displays the following output:

DHHH

Another syntax for removing substrings from a string, similar to the previous syntax, is:

expression [ delimiter, occurrence, fields ]

The bold brackets are part of the syntax and must be typed. Use this syntax to return the substring that is located between the stated number of occurrences of the specified delimiter. fields specifies the number of successive fields after the specified occurrence of the delimiter that are to be returned with the substring. The delimiter is part of the returned value when successive fields are returned. This syntax performs the same function as the FIELD function.

All substring syntaxes can be used with the assignment operator ( = ) to replace the value normally returned by the [ ] operator with the value assigned to the variable. For example:

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

returns the following:

A=   121212

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