Developer Guide and Reference

ID 767251
Date 10/31/2024
Public
Document Table of Contents

Character Substrings

A character substring is a contiguous segment of a character string. It takes one of the following forms:

v ([e1]:[e2])

a (s [, s] . . . ) ([e1]:[e2])

v

Is a character scalar constant, or the name of a character scalar variable or character structure component.

e1

Is a scalar integer (or other numeric) expression specifying the leftmost character position of the substring; the starting point.

e2

Is a scalar integer (or other numeric) expression specifying the rightmost character position of the substring; the ending point.

a

Is the name of a character array.

s

Is a subscript expression.

Both e1 and e2 must be within the range 1,2, ..., len, where len is the length of the parent character string. If e1 exceeds e2, the substring has length zero.

Description

Character positions within the parent character string are numbered from left to right, beginning at 1.

If the value of the numeric expression e1 or e2 is not of type integer, it is converted to integer before use (any fractional parts are truncated).

If e1 is omitted, the default is 1. If e2 is omitted, the default is len. For example, NAMES(1,3)(:7) specifies the substring starting with the first character position and ending with the seventh character position of the character array element NAMES(1,3).

Examples

Consider the following example:

  CHARACTER*8 C, LABEL
  LABEL = 'XVERSUSY'
  C = LABEL(2:7)

LABEL(2:7) specifies the substring starting with the second character position and ending with the seventh character position of the character variable assigned to LABEL, so C has the value 'VERSUS'.

Consider the following example:

  TYPE ORGANIZATION
    INTEGER ID
    CHARACTER*35 NAME
  END TYPE ORGANIZATION
  TYPE(ORGANIZATION) DIRECTOR
  CHARACTER*25 BRANCH, STATE(50)

The following are valid substrings based on this example:

  BRANCH(3:15)           ! parent string is a scalar variable
  STATE(20) (1:3)        ! parent string is an array element
  DIRECTOR%NAME(:)       ! parent string is a structure component

Consider the following example:

  CHARACTER(*), PARAMETER :: MY_BRANCH = "CHAPTER 204"
  CHARACTER(3) BRANCH_CHAP
  BRANCH_CHAP = MY_BRANCH(9:11)   ! parent string is a character constant

BRANCH_CHAP is a character string of length 3 that has the value '204'.