FORTRAN 77 Language Reference

Substrings

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 ] )

where

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:

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%