Common String Operations

Fortran supports many operators and intrinsic functions for manipulating strings.

Concatenation

Strings can be concatenated (pasted end-to-end) using the // operator:

            module constants
                integer, parameter :: MAX_NAME_LENGTH=20
            end module
            
            program string_example
                ! Variable definitions
                character(MAX_NAME_LEN) :: first_name, last_name
                character(MAX_NAME_LEN * 2) :: full_name
                
                ! Statements
                first_name = "Alfred"
                last_name = "Neumann"
                full_name = first_name // last_name
                print *, full_name
            end program
            

Output?

Trimming

One of the most common operations with strings is trimming, or removing the trailing spaces to get just the "meat". Trimming is done with the trim() intrinsic function. Note that we cannot change the maximum size of a string variable. The trim() function allocates a smaller temporary string, whose maximum length is the actual length of its argument.

            character(20) :: name
            
            name = 'Alfred Nobel'
            
            print *, name, ' is the namesake for the Nobel prize.'
            print *, trim(name), ' is the namesake for the Nobel prize.'
            
             Alfred Nobel         is the namesake of the Nobel prize.
             Alfred Nobel is the namesake of the Nobel prize.
            

We can use trimming to fix the issue with concatenating names above.

            module constants
                integer, parameter :: MAX_NAME_LENGTH=20
            end module
            
            program string_example
                ! Variable definitions
                character(MAX_NAME_LEN) :: first_name, last_name
                character(MAX_NAME_LEN * 2) :: full_name
                
                ! Statements
                first_name = "Alfred"
                last_name = "Neumann"
                full_name = trim(first_name) // ' ' // trim(last_name)
                print *, full_name
            end program
            
String length

Sometimes we want to know the length of a string. The len() and len_trim() functions return an integer length of the string variable and the actual content, respectively.

            character(20) :: name
            
            name = 'Alfred Nobel'
            
            print *, len(name)
            print *, trim_len(name)
            
             20
             12
            

trim_len(string) is equivalent to len(trim(string))

Substrings

Sometimes we want to extract part of a string. We can simply specify a starting and ending subscript separated by a ':'. If either is ommitted, and a ':' is present, the missing subscript is assumed to be 1 or the maximum subscript for the string.

This syntax can also be used on the left-hand-side of an assignment statement.

            character(MAX_NAME_LEN) :: name
            
            name = 'Alfred Pennyworth'
            
            print *, name(1:6)  ! Alfred
            print *, name(8:17) ! Pennyworth
            print *, name(:6)   ! Alfred
            print *, name(8:)   ! Pennyworth
            
            name(8:) = 'E. Neumann" ! name is now 'Alfred E. Neumann'