Sub-string ([ ]) operator takes a character expression and returns a sub-string of this expression:
The operator takes one or two comma-separated parameters:
If you specify only the first parameter, [ ] will return one single character.
For example, these code lines:
LET a = "Smith"
LET b = a[2]
DISPLAY b
will return m.
If you specify both first and last parameters, [ ] will return the character with the first number, the character with the last number, and all characters between them.
For example, these code lines:
LET a = "Smith"
LET b = a[2, 4]
DISPLAY b
will return mit.
first and last parameters must be greater than zero. first parameter must be in the range from 1 to length where length is the length of the character string. last must be no less than first and no greater than length.
Here is a simple example of how to use the sub-string ([ ]) operator:
MAIN
DEFINE a, b CHAR(5), c ARRAY [3, 4, 5] OF CHAR(5)
LET a = "Smith"
LET b = a[2, 4] -- mit
LET c[2, 2, 2] = a[3,4] -- it
DISPLAY "*** Sub-string Example ***" AT 1, 5
DISPLAY "LET a = \"Smith\"" AT 3, 5
DISPLAY "a: " ,a at 4, 5
DISPLAY "LET b = a[2, 4]" AT 6, 5
DISPLAY "b: " , b at 7, 5
DISPLAY "LET c[2, 2, 2] = a[3, 4]" AT 9, 5
DISPLAY "c: " , c[2, 2, 2] at 10, 5
CALL fgl_getkey()
END MAIN
A parameter is invalid for the sub-string ([ ]) operator if
In Querix 4GL, invalid operands produce runtime error -1332. A character variable has referenced subscripts that are out of range.
In some locales, the sub-string ([ ]) operator is byte-based. In East Asian locales that support multi-byte characters, 4GL automatically replaces any partial characters that this operator attempts to create with single-byte white-space characters so that no partial character is returned.