Share this page

# strtok

String Manipulation Functions

Returns a token from a string delimited by specified characters.

    char *strtok( char *string, const char *delimiters);

string | The string to scan.
delimiters | The string consisting of the character or characters that delimit tokens in the first string.

strtok returns a token from a string delimited by specified characters. After the first invocation, pass NULL as the value of `string` to get the next token.

## Return Values

On the first invocation, returns a pointer to the first token found in `string`. Each subsequent invocation with a NULL value of `string` returns the next token. Returns NULL when there are no more tokens to be found.

Insert the following at the beginning of a script that uses strtok:

`extern char* strtok(char *`token`, const char *`delimiter`);`

Strtok must be cast to a char pointer when assigning the return value, for example:

    char *token = (char *)strtok(path, separators);

## Example

In this example, strtok extracts the components of a file path. Multiple calls to strtok extract each successive component from the string, path.

The separators define how a token is demarcated i.e., they are used by strtok to find breaks between the tokens. The separators here are either `\' or `.'

When using multiple calls to strtok on the same string, specify the string only on the first invocation. Subsequent calls must pass NULL as the string parameter, as we do below.

        #include <string.h>
        char path[] = "c:\\mycomp\\lrun\\bin\\wlrun.exe";
        char separators[] = ".\\";
        char * token;

    // Get the first token

        token = (char *)strtok(path, separators);
        if (!token) {
            lr_output_message ("No tokens found in string!");
            return( -1 );
        }

    // While valid tokens are returned

        while (token != NULL ) {
            lr_output_message ("%s", token );

    // Get the next token

            token = (char *)strtok(NULL, separators);
        }

    Example: Output:
    Action.c(18): c:
    Action.c(18): mycomp
    Action.c(18): lrun
    Action.c(18): bin
    Action.c(18): wlrun
    Action.c(18): exe

Tips to improve your search results... VuGen Function Reference Version 26.1 Function Reference 26.1 All View Local Help Center View Online Help Center Version: