typestar

Character handling in Fortran

Fixed-length strings, and the trims and finds that make them behave.

program character_ops
    implicit none
    character(len=20) :: port = "Valparaiso"
    character(len=40) :: tag

    ! // concatenates; trim drops the padding a fixed length carries.
    tag = trim(port) // ", berth 9"
    print *, tag
    print *, "name length:  ", len_trim(port)
    print *, "finds 'para': ", index(port, "para")
    print *, "right-shifted: [", adjustr(port), "]"
end program character_ops

How it works

  1. character(len=20) pads with trailing blanks; trim drops them for splicing.
  2. // concatenates the trimmed port name onto its berth.
  3. len_trim, index, and adjustr measure, search, and shift the text.

Keywords and builtins used here

The run, in numbers

Lines
12
Characters to type
376
Tokens
70
Three-star pace
65 tpm

At the three-star pace of 65 tokens a minute, this run takes about 65 seconds.

Type this snippet

Step 1 of 3 in Strings & formats, step 16 of 27 in Language basics.

← Previous Next →