find_field(3f) - [M_strings:TOKENS] parse a string into tokens (LICENSE:MIT)
Synopsis
Description
Options
Examples
Author
License
Version
subroutine find_field (string, field, position, delims, delim, found)
character*(*),intent(in) :: string character*(*),intent(out) :: field integer,optional,intent(inout) :: position character*(*),optional,intent(in) :: delims character*(*),optional,intent(out) :: delim logical,optional,intent(out) :: found
Find a delimited field in a string.
Here is my equivalent, which I have used for nearly 2 decades, as you can see from the date. This does not try to mimic the C strtok (and does not have its limitations either). It is in a much more native Fortran style.
It is a little more complicated than some because it does some things that I regularly find useful. For example, it can tell the caller what trailing delimiter it found. This can be useful, for example, to distinguish between
somefield, someotherfieldversus
somefield=somevalue, someotherfieldAlso, I have a bit of special handling for blanks. All the usage information is in the argument descriptions. Note that most of the arguments are optional.
from comp.lang.fortran @ Richard Maine
STRING The string input. FIELD The returned field. Blank if no field found. POSITION On entry, the starting position for searching for the field. Default is 1 if the argument is not present. On exit, the starting position of the next field or len(string)+1 if there is no following field. DELIMS String containing the characters to be accepted as delimiters. If this includes a blank character, then leading blanks are removed from the returned field and the end delimiter may optionally be preceded by blanks. If this argument is not present, the default delimiter set is a blank. DELIM Returns the actual delimiter that terminated the field. Returns char(0) if the field was terminated by the end of the string or if no field was found. If blank is in delimiters and the field was terminated by one or more blanks, followed by a non-blank delimiter, the non-blank delimiter is returned. FOUND True if a field was found.
Sample of uses
program demo_find_field use M_strings, only : find_field implicit none character(len=256) :: string character(len=256) :: field integer :: position character(len=:),allocatable :: delims character(len=1) :: delim logical :: foundResults:delims=[,] position=1 found=.true. string=[a,b,[ccc,ddd],and more] write(*,(a))trim(string) do call find_field(string,field,position,delims,delim,found=found) if(.not.found)exit write(*,("<",a,">"))trim(field) enddo write(*,(*(g0)))repeat(=,70)
position=1 found=.true. write(*,(a))trim(string) do call find_field(string,field,position,[], ,delim,found=found) if(.not.found)exit write(*,("<",a,">",i0,1x,a))trim(field),position,delim enddo write(*,(*(g0)))repeat(=,70)
end program demo_find_field
> [a,b,[ccc,ddd],and more] > <> > <a> > <b> > <> > <ccc> > <ddd> > <> > <and more> > <> > ================================================================== > [a,b,[ccc,ddd],and more] > <>2 [ > <a>4 , > <b>6 , > <>7 [ > <ccc>11 , > <ddd>15 ] > <>16 , > <and>20 > <more>257 ] > ==================================================================
Richard Maine
version 0.1.0, copyright Nov 15 1990, Richard MaineMinor editing to conform to inclusion in the string procedure module
Nemo Release 3.1 | find_field (3m_strings) | January 10, 2025 |