RPG/400 Help

SUBST (Substring)

*---------*---------------*-----------------*----------------*---------------*
|   CODE  |    FACTOR 1   |     FACTOR 2    |     RESULT     |  INDICATORS   |
|         |               |                 |      FIELD     |               |
*---------*---------------*-----------------*----------------*---------------*
| SUBST   | Length to     | Base            | Target         |    _ ER _     |
| (P)     | extract       | string:start    | string         |               |
*---------*---------------*-----------------*----------------*---------------*

The SUBST operation returns a substring from factor 2, starting at the location specified in factor 2 for the length specified in factor 1, and places this substring in the result field. If factor 1 is not specified, the length of the string from the start position is used.

Factor 1 can contain the length value of the string to be extracted from the string specified in factor 2. It must be numeric with no decimal positions and can contain one of: a field name, array element, table name, literal, or named constant.

Factor 2 must contain either the base character string, or the base character string followed by ':', followed by the start location. The base string portion must be character, and can contain one of: a field name, array element, named constant, data structure name, table name, or literal. The start position must be numeric with zero decimal positions, and can contain one of the following: a field name, array element, table name, literal or named constant. If it is not specified, SUBST starts in position 1 of the base string.

The start location and the length of the substring to be extracted must be positive integers. The start location must not be greater than the length of the base string, and the length must not be greater than the length of the base string from the start location. If either or both of these conditions is not satisfied, the operation will not be performed, and if you specified an error indicator in positions 56 and 57 it will be set on. If you did not specify an error indicator, the exception/error handling routine receives control.

The result field must be character, and can contain one of the following: a field name, array element, data structure, or table name. The substring is left-justified, and its length should be at least as large as the length specified in factor 1. If the substring is longer than the field specified in the result field, the substring will be truncated from the right.

Note:
You cannot use figurative constants in the factor 1, factor 2, or result fields. No overlapping is allowed for factor 1 and the result field or factor 2 and the result field.

If factor 1 is shorter than the length of the result field, a P specified in the operation extender position (position 53) indicates that the result field should be padded on the right with blanks after the substring occurs.

Figure "SUBST Operation" shows examples of the SUBST operation.

SUBST Examples

Figure 57. SUBST Operation

*...1....+....2....+....3....+....4....+....5....+....6....+....7...
CL0N01N02N03Factor1+++OpcdeFactor2+++ResultLenDHHiLoEqComments++++++
C*
C* The SUBST operation extracts the substring from factor 2 starting
C* at position 3 for a length of 2.  The value 'CD' is placed in the
C* result field RESULT.  Indicator 90 is not set on because no error
C* occurred.
C                     Z-ADD3         T       20
C                     MOVEL'ABCDEF'  STRING 10
C           2         SUBSTSTRING:T  RESULT       90
C*
C* In this SUBST operation, the length is greater than the length
C* of the string minus the start position plus 1.  As a result,
C* indicator 90 is set on and the result field is not changed.
C*
C                     MOVE 'ABCDEF'  STRING  6
C                     Z-ADD4         T       10
C           5         SUBSTSTRING:T  RESULT       90
C*
C* In this SUBST operation, 3 characters are substringed starting
C* at the fifth position of the base string.  Because P is not
C* specified in position 53, only the first 3 characters of RESULT
C* are changed.  RESULT contains '123XXXXX'.
C                     Z-ADD3         LENGTH  20
C                     Z-ADD5         T       20
C                     MOVE 'TEST123' STRING  8
C                     MOVE *ALL'X'   RESULT
C           LENGTH    SUBSTSTRING:T  RESULT  8    90
C*
C* This example is the same as the previous one except P is
C* specified in position 53, and the result is padded with blanks.
C* RESULT equals '123bbbbb'.
C                     Z-ADD3         LENGTH  20
C                     Z-ADD5         T       20
C                     MOVE 'TEST123' STRING  8
C                     MOVE *ALL'X'   RESULT
C           LENGTH    SUBSTSTRING:T  RESULT  8 P  90
C*
C* In the following example, CITY contains the string
C* 'Toronto, Ontario'.  The SCAN operation is used to locate the
C* separating blank, position 9 in this illustration.  SUBST
C* without factor 1 places the string starting at position 10 and
C* continuing for the length of the string in field TCNTRE.
C* TCNTRE contains 'Ontario'.
C           ' '       SCAN CITY      C
C                     ADD  1         C
C                     SUBSTCITY:C    TCNTRE
C*
C* Before the operations STRING='bbbJohnbbbbbb'.
C* RESULT is a 10 character field which contains 'ABCDEFGHIJ'.
C* The CHECK operation locates the first nonblank character
C* and sets on indicator 10 if such a character exists.  If *IN10
C* is on, the SUBST operation substrings STRING starting from the
C* 'J' to the end of STRING.  Padding is used to ensure that
C* nothing is left from the previous contents of the result
C* field.
C* After the operations RESULT='Johnbbbbbb'.
C*
C           ' '       CHECKSTRING    ST             10
C   10                SUBSTSTRING:ST RESULT    P
 
 
 


[ Top of Page | Previous Page | Next Page | Table of Contents ]