Character declarations and substrings

A Fortran character value is a string of a length the type fixes at compile time. There is no terminator, no length stored beside the bytes and no way to resize a value once it exists. Everything else about strings follows: a shorter value is padded, a comparison ignores the padding, and building a string of a new length means concatenating into a new value. This lesson covers the length, the substring and the padding.

A fixed length

The length belongs to the type. character(len=12) declares a value of twelve characters, and character(len=*) takes the length from the initialiser or from the dummy argument at the call. A declaration without len= gives a length of one, which is the detail that makes character :: c a single character and not a string.

The program below declares twelve characters, stores seven, and asks for the length and for the substrings that make the string usable.

The file substrings.f90:

program substrings implicit none character(len=12) :: word character(len=5) :: part word = "Fortran" print *, "len(word) :", len(word) print *, "word :", "[" // word // "]" print *, "word(1:3) :", word(1:3) print *, "word(4:) :", word(4:) print *, "word(:3) :", word(:3) part = word(1:4) print *, "part :", "[" // part // "]" word(8:12) = " 2023" print *, "word :", "[" // word // "]" print *, "len_trim :", len_trim(word) print *, "trim :", "[" // trim(word) // "]" end program substrings

The square brackets are there because a Fortran string cannot show its own trailing blanks: every one of these lines would otherwise end at the same place, whatever the value holds.

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

len(word) : 12
word :[Fortran ]
word(1:3) :For
word(4:) :tran
word(:3) :For
part :[Fort ]
word :[Fortran 2023]
len_trim : 12
trim :[Fortran 2023]

Three facts are visible in that output. The value stored was seven characters and the declaration holds twelve, so five blanks follow it. A substring keeps the length it names, which is why part, a five character variable, holds "Fort " with a blank. And len reports the declared length while len_trim reports the length without trailing blanks, which are twelve and twelve here because the assignment filled the value to its end.

Substrings

A substring names a range of positions with the colon syntax. The first position is 1, the last is the length of the value, and both ends may be left out:

The bounds are checked only when the program is built with -fcheck=bounds; without it, s(0:3) reads whatever is before the string in memory. A substring is a value and not a view: part = word(1:4) copies four characters and pads them to the length of part.

Substring assignment

A substring on the left of an assignment is one of the few places Fortran lets a program change part of a value. The right side is padded with blanks or truncated to the length of the range, so word(8:12) = " 2023" fills five positions with a space and four digits, and the whole value becomes "Fortran 2023".

The assignment does not change the declared length, which is why appending to a string needs a new value of the new length: word = trim(word) // " more" is an error when the result is longer than the declaration, and the compiler reports it when both lengths are known. The lesson on building strings covers the patterns that grow a value, internal files and all.

Concatenation and padding

// joins two values into one whose length is the sum of the two lengths, blanks included. That is why "abc " // "abc " gives thirteen characters and not six, and why trim is in almost every concatenation a reader writes.

The second program shows the padding from the other side: two values that a reader would call equal, of different declared lengths.

The file padding.f90:

program padding implicit none character(len=5) :: a character(len=8) :: b a = "abc" b = "abc" print *, "a == b :", a == b print *, "a < b :", a < b print *, "len(a)/len(b):", len(a), len(b) print *, "len_trim(a) :", len_trim(a) print *, "trim(a) == b:", trim(a) == b print *, "a // b :", "[" // a // b // "]" end program padding

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

a == b : T
a < b : F
len(a)/len(b): 5 8
len_trim(a) : 3
trim(a) == b: T
a // b :[abc abc ]

Comparison

The comparison of two character values pads the shorter one with blanks and then compares, character by character, by the collating sequence. So "abc" in five characters equals "abc" in eight, and neither is less than the other: the padding makes them equal, which is what the first two lines of the output show.

Two rules follow and both cost a program an afternoon when forgotten:

The comparison operators are the ones from the operators lesson: ==, /=, <, <=, >, >=, with the old dot forms still accepted.

Exercises

  1. Print len(word(4:9)) for a twelve character value. Why is it not 6?
  2. Assign word(1:20) = "x" and build the program. What does the compiler say?
  3. Make a and b equal in length and compare them again. Which line of the output changes?
  4. Print index("Fortran 2023", "2023"). Which position does it report, and why?

Questions

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

  1. Where does the length of a character value live?

    Answer

    In its type, fixed at compile time

    Every character value has its declared length, and there is no terminator.

  2. What does character :: c declare?

    Answer

    A single character

    The default length is one, so the declaration holds one character.

  3. What is the length of word(4:9)?

    Answer

    6

    The range is inclusive on both ends, so 9 - 4 + 1 is 6.

  4. What does word(4:) mean?

    Answer

    From position 4 to the end of the value

    A missing second bound runs to the declared length.

  5. How are two character values of different lengths compared?

    Answer

    The shorter one is padded with blanks, then the two are compared

    Padding to a common length makes "abc" equal to "abc ".

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

    Answer

    2 6

    len_trim ignores the four trailing blanks and len reports the declaration.

  7. What is the length of "abc" // "de"?

    Answer

    5

    Concatenation adds the two lengths, and neither literal has padding of its own.

  8. What does word(8:12) = " 2023" do to a twelve character value?

    Answer

    It replaces five positions and keeps the rest

    A substring on the left assigns into the range and leaves the other positions alone.

  9. Why is "ABC" == "abc" false?

    Answer

    Character comparison is case sensitive

    The collating sequence puts A before a, so the first characters differ.

  10. What does trim(s) return?

    Answer

    A copy of s without its trailing blanks

    trim shortens the value, which is why it appears before a concatenation.

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