8.266 SPLIT — Parse a string into tokens, one at a time

Synopsis:

RESULT = SPLIT(STRING, SET, POS [, BACK])

Description:

Updates the integer POS to the position of the next (or previous) separator in STRING.

If BACK is absent or is present with the value false, POS is assigned the position of the leftmost token delimiter in STRING whose position is greater than POS, or if there is no such character, it is assigned a value one greater than the length of STRING. This identifies a token with starting position one greater than the value of POS on invocation, and ending position one less than the value of POS on return.

If BACK is present with the value true, POS is assigned the position of the rightmost token delimiter in STRING whose position is less than POS, or if there is no such character, it is assigned the value zero. This identifies a token with ending position one less than the value of POS on invocation, and starting position one greater than the value of POS on return.

Class:

Subroutine

Arguments:
STRINGShall be of type CHARACTER.
SETShall be of type CHARACTER.
POSShall be of type INTEGER.
BACK(Optional) Shall be of type LOGICAL.
Example:
character(len=:), allocatable :: input
character(len=2) :: set = ', '
integer :: p
input = "one,last example"
p = 0
do
  if (p > len(input)) exit
  istart = p + 1
  call split(input, set, p)
  iend = p - 1
  print '(t7, a)', input(istart:iend)
end do
Standard:

Fortran 2023

See also:

SCAN — Scan a string for the presence of a set of characters