Strings: length, TRIM, concatenation

A Fortran string has a length that is part of its type, and every operation keeps that length: an assignment pads with blanks or truncates, a concatenation adds the two lengths, and a comparison ignores the blanks at the end. The characters lesson introduced the declaration and the substring; this one is about the length, which is where most string bugs live.

A fixed length

character(len=10) :: name holds exactly ten characters at every moment. The assignment name = "Fortran" puts seven letters in the first seven places and three blanks in the rest, and nothing in the program can change the ten.

The file strings.f90 prints the string between brackets so the blanks are visible:

program strings_demo implicit none character(len=10) :: name character(len=:), allocatable :: built name = "Fortran" print *, "name : [", name, "]" print *, "len :", len(name) print *, "len_trim:", len_trim(name) print *, "trim : [", trim(name), "]" built = trim(name) // " 2023" print *, "built : [", built, "] len =", len(built) end program strings_demo

gfortran -Wall -Wextra -std=f2018 strings.f90 -o strings
./strings

name : [Fortran ]
len : 10
len_trim: 7
trim : [Fortran]
built : [Fortran 2023] len = 12

len is the declared length and len_trim is the number of characters before the trailing blanks: ten and seven. The two functions answer different questions, and a program that mixes them up prints a string with three invisible blanks on the end.

TRIM and LEN_TRIM

trim(s) returns the string without its trailing blanks, and the result has the length len_trim(s). It removes blanks only at the end: a blank at the start is data, and a blank between words is data. len_trim(s) on its own is the cheap way to ask how long the useful part is without building a new string.

Neither one changes the variable it is given: Fortran has no in-place trim, and name = trim(name) pads the right side back to ten blanks on assignment, so it changes nothing. The trimmed value is useful in an expression, not as a statement.

Concatenation and the deferred length

// joins two strings and the result has the sum of their lengths. Without trim, joining name with " 2023" would give Fortran followed by five more characters, blanks in the middle of the result. With trim the pieces meet.

character(len=:), allocatable :: built declares a string whose length is decided by its value: the assignment allocates exactly twelve characters here, seven of the name and five of the year. It is the string to use when the length is computed, and the allocatable lesson rules apply to it unchanged.

Padding and comparison

A comparison between two strings of different lengths pads the shorter one with blanks, so "abc" == "abc " is true and "abc" == "abc x" is not. That rule makes a fixed-length string behave like a value, and it is why a length of eight compares correctly with a three-letter literal.

The other half of the rule is truncation: an assignment to a shorter string keeps the first characters and drops the rest, and -Wall warns about it when the compiler can see the lengths.

program pad_demo implicit none character(len=8) :: a, b character(len=3) :: c a = "abc" b = "abc x" c = "abcdef" print *, "equal abc :", a == "abc" print *, "equal abc+3sp :", a == "abc " print *, "b : [", b, "]" print *, "c : [", c, "]" print *, "adjustl : [", adjustl(" ab"), "]" print *, "adjustr : [", adjustr("ab "), "]" print *, "len_trim(a) :", len_trim(a) end program pad_demo

gfortran -Wall -Wextra -std=f2018 pad.f90 -o pad
./pad

pad.f90:8:14:

8 | c = "abcdef"
| 1
Warning: CHARACTER expression will be truncated in assignment (3/6) at (1) [-Wcharacter-truncation]
equal abc : T
equal abc+3sp : T
b : [abc x ]
c : [abc]
adjustl : [ab ]
adjustr : [ ab]
len_trim(a) : 3

The warning names the lengths, three and six, and the string keeps the first three letters. Both comparisons are true, and b shows that the blanks in the middle are kept: the eight characters are abc, three blanks and an x.

ADJUSTL and ADJUSTR

adjustl moves the leading blanks to the end and adjustr moves the trailing blanks to the front; neither changes the length. They are the pair to reach for when a value arrives right-aligned from a formatted read and the program wants to compare it or concatenate it.

An assumed-length dummy character(len=*) takes the length of what the caller passed. The file len.f90 passes a variable and a literal to the same procedure:

program len_demo implicit none character(len=12) :: s s = "hello" call show(s) call show("longer text here") contains subroutine show(t) character(len=*), intent(in) :: t print *, "len =", len(t), " len_trim =", len_trim(t), " [", trim(t), "]" end subroutine show end program len_demo

gfortran -Wall -Wextra -std=f2018 len.f90 -o len
./len

len = 12 len_trim = 5 [hello]
len = 16 len_trim = 16 [longer text here]

The same twelve characters arrive from the variable, three of them blanks, and sixteen from the literal, which has no blanks at all. That is how a procedure accepts any string without copying it, and it is the form to prefer over a fixed length in an argument list.

Exercises

  1. Print len(name // "!") for the ten-character name. Why is the answer eleven and not fourteen?
  2. Assign "abcdefghijkl" to name. What does the program print, and what does the compiler say?
  3. Compare "ab" with "ab " and with " ab". Which comparison is true, and what does adjustl change?
  4. Make built a fixed-length string of twenty characters and print len_trim(built). What does the extra length cost?

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What does LEN of a character(len=10) variable return?

    Answer

    10, whatever the value is

    The length is part of the type and never changes.

  2. What does trim(s) remove?

    Answer

    The trailing blanks

    TRIM cuts the blanks at the end only.

  3. What does this program print? program p implicit none character(len=6) :: s s = "abc" print *, len_trim(s), len(s) end program p

    Answer

    3 6

    Three characters are used and the declared length is six.

  4. What is the length of trim("abc ") // "de"?

    Answer

    5

    The trim gives three characters and the second part two.

  5. Which declaration gives a string whose length is set by its value?

    Answer

    character(len=:), allocatable :: s

    A deferred length is written as a colon and needs an allocatable variable.

  6. Is "abc" == "abc " true?

    Answer

    Yes, the shorter string is padded with blanks

    Comparison pads the shorter operand with blanks.

  7. What happens when a six-character value is assigned to a three-character string?

    Answer

    The first three characters are kept and -Wall warns

    The length is fixed, so the rest is truncated with a warning.

  8. What does adjustl(" ab") return?

    Answer

    "ab "

    The leading blanks move to the end and the length stays the same.

  9. What does character(len=*) mean for a dummy argument?

    Answer

    The length is taken from the actual argument

    An assumed-length dummy accepts a string of any length.

  10. Why does trimming a variable with s = trim(s) change nothing for a fixed-length s?

    Answer

    The assignment pads the value back to the declared length

    A fixed length is restored by the blank padding of the assignment.

The programs on this page were built with GNU Fortran (Alpine 15.2.0) 15.2.0 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.

Article author: Arthur Isaev