A character datum is a sequence of one or more characters. A character substring is a contiguous portion of a character variable or of a character array element or of a character field of a structured record.
A substring name can be in either of the following two forms:
v( [ e1 ] : [ e2 ] )
a( s [, s ] ) ( [ e1 ] : [ e2 ] )
v |
Character variable name |
a(s [, s] ) |
Character array element name |
e1 |
Leftmost character position of the substring |
e2 |
Rightmost character position of the substring |
:
Both e1 and e2 are integer expressions. They cannot exceed the range of INTEGER*4 on 32-bit environments. If the expression is not in the range (-2147483648, 2147483647), then the results are unpredictable. When compiled for 64-bit environments, the substring character position expressions can be in the range of INTEGER*8.
Example: The string with initial character from the Ith character of S and with the last character from the Lth character of S:
S(I:L)
In the above example, there are L-I+1 characters in the substring.
The following string has an initial character from the Mth character of the array element A(J,K), with the last character from the Nth character of that element.
A(J,K)(M:N)
In the above example, there are N-M+1 characters in the substring.
Here are the rules and restrictions for substrings:
Character positions within a substring are numbered from left to right.
The first character position is numbered 1, not 0.
The initial and last character positions must be integer expressions.
If the first expression is omitted, it is 1.
If the second expression is omitted, it is the declared length.
The result is undefined unless 0 < I £ L£ the declared length, where I is the initial position, and L is the last position.
Substrings can be used on the left and right sides of assignments and as procedure actual arguments.
Substrings must not be overlapping. ASTR(2:4) = ASTR(3:5) is illegal.
Examples: Substrings--the value of the element in column 2, row 3 is e23:
demo% cat sub.f character v*8 / 'abcdefgh' /, & m(2,3)*3 / 'e11', 'e21', & 'e12', 'e22', & 'e13', 'e23' / print *, v(3:5) print *, v(1:) print *, v(:8) print *, v(:) print *, m(1,1) print *, m(2,1) print *, m(1,2) print *, m(2,2) print *, m(1,3) print *, m(2,3) print *, m(1,3)(2:3) end demo% f77 sub.f sub.f: MAIN: demo% a.out cde abcdefgh abcdefgh abcdefgh e11 e21 e12 e22 e13 e23 13 demo%