The Queen's University of Belfast

Parallel Computer Centre
[Next] [Previous] [Top]
Character processing
Character Processing
Topics
- Character type.
- Character constants.
- Character variables.
- Character manipulation.
Character type
Single characters and strings
Character is an intrinsic data type and may appear as:
- Single characters.
- Strings of characters.
- Character strings have a length.
- Characters within a string are referred to by position:

Character Constants
Character (or literal) constants are defined by enclosing text within apostrophes.
- Either single quotes `...' or double quotes "..."
- Character constants are most often used to enhance I/O:
WRITE(6,*) `Enter value for radius`
READ(5,*) radius
area = pi*radius*radius
WRITE(6,*) `Area of circle r=', &
radius, ` is `, area
Which appears on screen as:
Enter value for radius
12.0
Area of circle r= 12.0 is 452.38925
- If the string contains a type of delimiter:
- use the other to enclose the string, for example:
"This ` is an apostrophe"
`This " is a double quote'
- To consistently use one delimiter it may be included in string as a double character, for example:
"This ` and "" are both types"
Character Variables
Specification
CHARACTER :: yesorno, sex='F'
- Strings:
- maximum length for the string is specified using
len=
CHARACTER(len=12) :: surname
CHARACTER(len=6) :: initials, title
title = `Prof.'
initials = `FJS'
surname = `Bloggs'
CHARACTER [(len= )] [,attr] :: name
Character manipulation
Concatenation
The only operator which may be applied to character strings in an expression is the conconcatenation operator //.
- Strings may be concatenated as follows:
CHARACTER(len=6) :: surname
CHARACTER (len=24) :: name
surname = `Bloggs'
name = `Dr `//`Fred `//surname
- Truncation and padding for strings of different lengths:
- if the right hand side is shorter the result is truncated.
- if the left hand string is shorter - the result is padded with blanks.
Substrings
Substrings are sections of larger strings.
- Selected by position with respect to the leftmost character:
name( [start]:[stop] )
CHARACTER(len=7) :: lang=`Fortran'
lang(2:4) !ort
lang(6:6) !a
lang(:4) !Fort
lang(6:) !an
lang(:) !Fortran
lang(10:) !zero length string.
lang(5:10) !illegal 10>len
Intrinsic functions
- Intrinsic function, example:
LEN(string)
- returns string length.
INDEX(sub,string)
- returns the position of string within string, or 0.
TRIM(string)
- returns the string without trailing blanks.
- Example:
CHARACTER(len=12) :: surname, firstname
INTEGER :: length, pos
...
length = LEN(surname) !len=12
firstname = `Walter`
pos = INDEX(firstname, `al`) !pos=2
firstname = `Fred`
pos = INDEX(firstname, `al`) !pos=0
length = LEN(TRIM(firstname)) !len=4
[Next] [Previous] [Top]
All documents are the responsibility of, and copyright, © their authors and do not represent the views of The Parallel Computer Centre, nor of The Queen's University of Belfast.
Maintained by Alan Rea, email A.Rea@qub.ac.uk
Generated with CERN WebMaker