String tools

Contents
Trim and len_trim
Adjustl and adjustr
Index and scan
Verify sets

Trim and len_trim

Fortran pads short strings with blanks, which breaks comparisons and messages. Len reports the declared length while len_trim reports the length without trailing blanks. Trim removes the padding when building output.

program trim_demo implicit none character(len=20) :: s = "hello" print *, len(s) print *, len_trim(s) print *, "[" // trim(s) // "]" end program trim_demo

Adjustl and adjustr

Adjustl moves text to the left and adjustr moves it to the right. Use them to align columns in reports without manual blank counting.

program adjust_demo implicit none character(len=20) :: s = "hello" print *, "[" // adjustl(s) // "]" print *, "[" // adjustr(s) // "]" end program adjust_demo

Index and scan

Index finds the first occurrence of a substring and returns 0 when it is absent. Scan finds the first character from a set, which suits delimiter searches. Both return positions, not the text itself.

program find_demo implicit none character(len=32) :: line = "nx=200, tol=1.0e-8" print *, index(line, "tol") print *, scan(line, ",=") end program find_demo

Verify sets

Verify checks that every character belongs to an allowed set and returns 0 when the string is clean. A nonzero result is the position of the first bad character. This makes it ideal for digit checks and input validation.

program verify_demo implicit none character(len=16) :: code = "20261006" if (verify(code, "0123456789") == 0) write(*, "(a)") "digits only" end program verify_demo

Next: Command-line arguments

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: