Character strings

Contents
Fixed length
Concatenate //
Trim and adjustl
Compare strings

Fixed length

A character variable has a fixed length set at declaration: character(len=20) reserves twenty slots no matter how short the text is.

Short values are padded with blanks, so len always reports 20 while len_trim counts only up to the last visible character.

program fixed implicit none character(len=20) :: name name = "Ada" print *, len(name) print *, len_trim(name) end program fixed

./fixed

20
3

Concatenate //

The // operator glues strings together: first // blank // last builds one longer value from three pieces.

Joining exact length pieces avoids stray blanks, so the result prints as Ada Lynn.

program concat implicit none character(len=3) :: first character(len=4) :: last first = "Ada" last = "Lynn" print *, first // " " // last end program concat

./concat

Ada Lynn

Trim and adjustl

Padding blanks get in the way of brackets and messages, so trim cuts trailing blanks and adjustl pushes text to the left.

Brackets make the blanks visible: the raw value keeps its leading blanks while the adjusted one starts at once.

program tidy implicit none character(len=10) :: raw raw = " Ada" print *, len_trim(raw) print *, "[" // raw // "]" print *, "[" // adjustl(raw) // "]" end program tidy

./tidy

6
[ Ada ]
[Ada ]

Compare strings

Comparison operators test equality and order: == checks for equal text while /=, <, and > cover the remaining cases.

Shorter values are padded with blanks before the test, so same length variables compare exactly as written.

program compare implicit none character(len=4) :: a character(len=4) :: b a = "Ada" b = "Bob" if (a == b) then print *, "same" else print *, "different" end if end program compare

./compare

different

Next: Formatted IO

Article author: Arthur Isaev

Related articles
Fortran
Fortran course: zero to hero in 50 lessons
What is Fortran and where it runs
Install gfortran
Your first program
Program structure
Types: integer real complex logical character
Kind parameters
Operators
Conditions
DO loops
Arrays
Array sections
Character strings
Formatted IO
Files
Subroutines and functions
Modules
USE with ONLY
INTENT
OPTIONAL and PRESENT
Explicit interfaces
Derived types
Type-bound procedures
Object orientation
Operator overloading
Finalization
Allocatable arrays
Pointers in Fortran
DO CONCURRENT
Coarrays
OpenMP basics
Namelists
IEEE arithmetic
Intrinsic matrix math
Random numbers
String tools
Command-line arguments
Environment variables
error stop
Testing
Build with gfortran and make
Debugging
C interoperability
From fixed form to free form
Numeric models
Performance
Modern Fortran tour: 2008, 2018, 2023
Capstone: stats tool
Capstone: 1D heat equation
Style guide
Hero roadmap: the whole course on one page
Install gfortran on Windows 11
Install gfortran on Ubuntu
Install gfortran on Rocky Linux
Install gfortran on macOS
Install gfortran on FreeBSD
Fortran in Docker

Search this site

Channel @aofeed Chat @aofeedchat

Contacts and cooperation:
I recommend our hosting beget.com
Write to info@aredel.com if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: