Skip to content

1. A first string ​

report starts with its title. A string is declared like any other variable and it has no length to choose: it takes the length of what is assigned to it.

f90
program report
!< Tutorial, chapter 1: a first string.
use stringifor
implicit none
type(string) :: title, subtitle, heading

title = 'cars report'                       ! from a character, of any length
subtitle = title                            ! from another string
print '(A)', title//''                      ! // gives a character
print '(A)', title%chars()                  ! so does chars()
print '(DT)', title                         ! defined I/O

heading = title%startcase()//', year '//'2001'
print '(A)', heading//''
print '(A,I0)', 'length: ', heading%len()

print '(A,L1)', 'same title? ', title == subtitle
print '(A,L1)', 'same as a character? ', title == 'cars report'
print '(A,L1)', 'sorts before "dogs"? ', title < 'dogs'

subtitle = title .cat. ', draft'            ! .cat. gives a string
print '(A)', subtitle%upper()//''
endprogram report
  • A string is assigned from a character of any length, or from another string.
  • // concatenates strings and characters and gives a standard character: this is how a string goes wherever Fortran expects a character, a print included. title//'' is the shortest way to say "the characters of title".
  • chars() returns the same characters; the DT edit descriptor prints the string through its defined I/O.
  • .cat. concatenates like //, but gives a string.
  • The comparison operators work between strings, and between a string and a character.
  • The methods are called on the variable: title%startcase(), heading%len(), subtitle%upper(). They return a new value and leave the string unchanged.

Running it ​

$ report
cars report
cars report
cars report
Cars Report, year 2001
length: 22
same title? T
same as a character? T
sorts before "dogs"? T
CARS REPORT, DRAFT

What you learned

A string has no fixed length; // turns it into a character and .cat. keeps it a string; methods return new values. Reference: Strings and I/O.

Next: 2. Cleaning and transforming.