Appearance
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
stringis assigned from acharacterof any length, or from anotherstring. //concatenates strings and characters and gives a standardcharacter: this is how a string goes wherever Fortran expects a character, aprintincluded.title//''is the shortest way to say "the characters oftitle".chars()returns the same characters; theDTedit descriptor prints the string through its defined I/O..cat.concatenates like//, but gives astring.- 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, DRAFTWhat 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.