Character substrings

A character substring is a contiguous portion of a character string (called a parent string), which is a scalar variable name, scalar constant, scalar structure component, or array element. A character substring is identified by a substring reference whose form is:
Read syntax diagramSkip visual syntax diagram
>>-+-scalar_variable_name-+------------------------------------->
   +-array_element--------+   
   +-scalar_constant------+   
   '-scalar_struct_comp---'   

>--(--+-----------+--:--+-----------+--)-----------------------><
      '-int_expr1-'     '-int_expr2-'      

int_expr1 and int_expr2
specify the leftmost character position and rightmost character position, respectively, of the substring. Each is a scalar integer expression called a substring expression.

The length of a character substring is the result of the evaluation of MAX(int_expr2 - int_expr1 + 1,0).

If int_expr1 is less than or equal to int_expr2, their values must be such that:

where length is the length of the parent string. If int_expr1 is omitted, its default value is 1. If int_expr2 is omitted, its default value is length.

FORTRAN 77 does not allow character substrings of length 0. Fortran 90 and up does allow these substrings. To perform compile-time checking on substring bounds in accordance with FORTRAN 77 rules, use the -qnozerosize compiler option. For Fortran 90 compliance, use -qzerosize. To perform run-time checking on substring bounds, use both the -qcheck option and the -qzerosize (or -qnozerosize) option. (See the XL Fortran Compiler Reference for more information.)

A substring of an array section is treated differently. See Substring ranges.

Examples

CHARACTER(8) ABC, X, Y, Z
ABC = 'ABCDEFGHIJKL'(1:8)   ! Substring of a constant
X = ABC(3:5)                ! X = 'CDE'
Y = ABC(-1:6)               ! Not allowed in either FORTRAN 77 or Fortran 90
Z = ABC(6:-1)               ! Z = '' valid only in Fortran 90