Substrings

In any context in which you reference a character field, you can reference a substring, which is a sequential subset of the characters in that field. If a field value is ABCD, you can reference (for example) BC, which is the second and third character.

In addition, you can specify a substring on the left side of an assignment statement if the target field is of type CHAR, DBCHAR, or UNICODE. The substring area is filled (padded with blanks, if necessary), and the assigned text does not extend beyond the substring area (but is truncated, if necessary). In addition, you can specify a substring on the left side if the target field is a limited-length string; that situation is described later, by example.

The syntax of a substring reference is as follows.

Substring syntax diagram
itemReference
An character or HEX field, but not a literal. The item may be a system variable or an array element. Special considerations apply to limited-length strings, as described later.
fromIndex
The first character of interest in the item, where 1 represents the first character in the character item, 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.

The value of fromIndex represents a byte position unless itemReference refers to an item of type DBCHAR or UNICODE, in which case the value represents a double-byte character position.

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

When itemReference is a limited-length string, the value of fromIndex is between 1 and the length you specified in the variable declaration. If the position has no value, the substring is an empty string, as shown in a later example.

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

toIndex
The last character of interest in the item, where 1 represents the first character in the character item, 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.

The value of toIndex represents a byte position unless itemReference refers to an item of type DBCHAR or UNICODE, in which case the value represents a double-byte character position.

When itemReference is a string (not limited length), the maximum value of toIndex is the position of the last character in the string.

When itemReference is a limited-length string, the maximum value of toIndexis the length specified in the variable declaration. If the position has no value, the substring extends only to the last position that holds a character, as shown in a later example.

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

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

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 substring 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"
:

Related concepts
References to variables in EGL

Related tasks
Syntax diagram for EGL statements and commands

Related reference
Text expressions

Feedback
(C) Copyright IBM Corporation 2000, 2005. All Rights Reserved.