# STRTOK

> [HTML Version](strtok_sbr.htm)

_Reviewed  May 2025_

**xcall STRTOK, opcode, subject, fdelim, fld1 \{, rdelim, fld2, ...fldn\}**

STRTOK parses the _subject _variable according to the delimiters specified, and returns one or more fields in the _fld1...fldn_ variables. You can call it once for each field (and change the field delimiter characters as you go), or have it return several parsed fields in one call, ending either when it hits a delimiter in the _rdelim _field or runs out of _subject _or of _fldx _parameters. 

STRTOK is both convenient and very efficient (compared to the equivalent logic in BASIC) for parsing string data, but may take a little practice to get used to. For a couple of real examples using it, see [Almost Comma Delimited](almostcommadelimited.md) and [Large Packets, Multiple Delimiters](largepacketsmultiple.md).

**Parameters**

_opcode_  ([Num](num.md))  \[in/out\]

should be set to 0 for the initial call, and 1 for subsequent calls using the original _subject_. It will be updated automatically from 0 to 1 to make this easy. In addition, you may specify one or more of the following values for variations of the behavior:

| **Value** | **Search Type** |
|------|------|
| \&h0002 | Ignore delimiters in _subject _if they are inside pairs of quotes ("). Quotes surrounding a field are removed. |
| \&h0004 | Ignore quotes in the middle of fields in _subject_. For example, in a CSV list (comma delimited) containing \{ 6" wrench, 2" screw, "foo,bar", ... \},** ** the quotes in the first two items would be treated as normal characters. |




_subject  _([String](string.md))  \[in/out\]

is the string to be parsed. Must be null terminated\! _subject _will be modified by the routine, which will replace the delimiter characters with null bytes.

_fdelim  _([String](string.md))  \[in\]

is a list of one or more field delimiter characters.

_rdelim  _([String](string.md))  \[in/out\]

is a list of one or more "record" delimiter characters.

(The subroutine will terminate when it hits one of these, whereas it will keep going after each field delimiter until all the FLDx parameters are used up.) Parameter is irrelevant if you just want to get one field. Even in the case of multiple fields, you can set it to "". Note, however, that if specified, it will be returned updated with the actual last delimiter character processed (i.e. the one that terminated the subroutine). This can be very useful when there are more than one possible delimiter, or when you want to determine whether you got a complete record or just ran out of FLDx parameters. If you do not care about record delimiters, then specify it as a literal "" (in which case it cannot be updated) or remember to clear it prior to each XCALL STRTOK. Otherwise, it will get updated to match the field delimiter. The subroutine should be smart enough to ignore record delimiters that are also field delimiters, but it could nonetheless lead to confusion.

Note that prior to Build 905.3, _rdelim _was assumed to be 2 or more bytes and the second byte was getting cleared, which would have clobbered the next variable if rdelim only mapped as one byte.

_fld1...fldn_  ([String](string.md))  \[out\]

will return the parsed fields or tokens from _subject_, according to the specified delimiters.

**Subtopics**

- [Almost Comma Delimited](almostcommadelimited.md)

- [Large Packets, Multiple Delimiters](largepacketsmultiple.md)