Chapter 25. Strings

Table of Contents

25.. Motivation
25.. Defining String Variables
25.. String Constants
25.. Truncation and Padding
25.. Common String Operations
Concatenation
Trimming
String length
Substrings
25.. Strings as Subprogram Arguments
25.. Command-line Arguments
25.. Code Quality
25.. Performance
Code Examples
25.. Self-test

Motivation

Most programs need to communicate with end users. To facilitate this, we have character sets such as ASCII, ISO-Latin1, and Unicode. A sequence of such characters is called a string.

All input from and output to a terminal is in the form of a strings, even if that input and output is numeric. When you type in the number 451.32 as input to a program, you are sending the program the characters '4', '5', '1', '.', '3', and '2'. The Fortran read subroutine then converts this string of characters to the appropriate binary format (usually two's complement or IEEE floating point) and stores it in some variable.

Some programs are meant to process character data, not numeric data. For example, a program to manipulate genetic data might use strings of 'a' for adenine, 'c' for cytosine, 'g' for guanine, 't' for thymine (and perhaps 'u' for uracil, which is almost identical to thymine).