Substrings

In any context where you refer to a character variable, you can refer instead to a substring (a sequential subset of the characters in that variable). For example, if a variable named myVar has the value is "ABCD", you can refer to "BC", the second and third characters, as myVar[2:3].

In addition, you can specify a substring on the left side of an assignment statement if the target substring has a fixed length. The substring area is padded with blanks if the source is shorter than the substring, and the source text is truncated if it is longer than the substring.

EGL provides a number of system functions that work with substrings:

Syntax

Substring syntax diagram
textVariable
A character or HEX variable, but not a literal. The variable can be a system variable or an array element. Special considerations apply to limited-length strings, as described later.
fromIndex
The first character to excerpt from the variable, where 1 is the index of the first character in the variable. You can use a numeric expression that resolves to an integer, but the expression cannot include a function invocation.
toIndex
The last character of interest in the variable, where 1 again represents the first character in the character variable, 2 represents the second, and so on. You can use a numeric expression that resolves to an integer, but the expression cannot include a function invocation.

If toIndex is greater than fromIndex and both numbers are valid, the substring is an empty string.

The value of either index represents a byte position unless textVariable is a DBCHAR or UNICODE type. In that case, the value represents a double-byte character position.

When textVariable is a string (not limited length), the value of the index is between 1 and the length of the string.

When textVariable is a limited-length string, the value of the index is between 1 and the length you specified in the variable declaration. If there are no characters at the specified positions, the contents of the target substring depend on the target type. If the target type is STRING, the result is an empty string; otherwise the target is padded with blanks.

Count from the leftmost character, even if you are working with a bidirectional language such as Arabic or Hebrew.

Examples

Consider the following example:
  limited string(20); 	
  s string;  	
  limited = "123456789"; 	
  s = limited[11:12]; // No error, and value of s is "" (an empty string).
  s = limited[8:12];  // No error, and value of s is "89". 	
  limited = s[8:12];  // Error because s has no length limit.  
                      // The last valid position is the one with the last character.
If you use a substring from a limited-length string on the left side of an assignment statement and if fromIndex is beyond the position that contains the last character, EGL substitutes a blank for each null character that is between the value already in the limited-length string and the assigned content:
  limited string(20) = "123456789";	
  s string = "abc";
  limited[12:14] = s; // no error; value of limited becomes "123456789  abc"
:

Feedback