strtok(3f) - [M_strings:TOKENS] Tokenize a string (LICENSE:PD)
Synopsis
Description
Options
Returns
Examples
Author
License
function strtok(source_string,itoken,token_start,token_end,delimiters) result(strtok_status)
! returned value logical :: strtok_status ! string to tokenize character(len=*),intent(in) :: source_string ! token count since started integer,intent(inout) :: itoken ! beginning of token integer,intent(out) :: token_start ! end of token integer,intent(inout) :: token_end ! list of separator characters character(len=*),intent(in) :: delimiters
The STRTOK(3f) function is used to isolate sequential tokens in a string, SOURCE_STRING. These tokens are delimited in the string by at least one of the characters in DELIMITERS. The first time that STRTOK(3f) is called, ITOKEN should be specified as zero. Subsequent calls, wishing to obtain further tokens from the same string,This routine assumes no other calls are made to it using any other input string while it is processing an input line.
should pass back in TOKEN_END and ITOKEN until the function result returns .false.
source_string input string to parse itoken token count should be set to zero for a new string delimiters characters used to determine the end of tokens
token_start beginning position in SOURCE_STRING where token was found token_end ending position in SOURCE_STRING where token was found strtok_status
Sample program:
program demo_strtok use M_strings, only : strtok implicit none character(len=264) :: inline character(len=*),parameter :: delimiters= ;, integer :: iostat, itoken, ibegin, iend do ! read lines from stdin until end-of-file or error read (unit=*,fmt="(a)",iostat=iostat) inline if(iostat /= 0)stop ! must set ITOKEN=0 before looping on strtok(3f) ! on a new string. itoken=0 do while & &( strtok(inline,itoken,ibegin,iend,delimiters) ) print *, itoken,& & TOKEN=[//(inline(ibegin:iend))//],ibegin,iend enddo enddo end program demo_strtoksample input file
this is a test of strtok; A:B :;,C;;
sample output file
1 TOKEN=[this] 2 5 2 TOKEN=[is] 7 8 3 TOKEN=[a] 10 10 4 TOKEN=[test] 12 15 5 TOKEN=[of] 17 18 6 TOKEN=[strtok] 20 25 7 TOKEN=[A:B] 28 30 8 TOKEN=[:] 32 32 9 TOKEN=[C] 35 35
John S. Urban
Public Domain
Nemo Release 3.1 | strtok (3m_strings) | January 10, 2025 |