RAD Studio
ContentsIndex
PreviousUpNext
strtok, _mbstok, wcstok

Header File 

string.h, mbstring.h 

Category 

Memory and String Manipulation Routines 

Prototype 

char *strtok(char *s1, const char *s2); 

wchar_t *wcstok(wchar_t *s1, const wchar_t *s2); 

unsigned char *_mbstok(unsigned char *s1, const unsigned char *s2); 

Description 

Searches one string for tokens, which are separated by delimiters defined in a second string. 

strtok considers the string s1 to consist of a sequence of zero or more text tokens, separated by spans of one or more characters from the separator string s2. 

The first call to strtok returns a pointer to the first character of the first token in s1 and writes a null character into s1 immediately following the returned token. Subsequent calls with null for the first argument will work through the string s1 in this way, until no tokens remain. 

The separator string, s2, can be different from call to call.

Note: Calls to strtok cannot be nested with a function call that also uses strtok. Doing so will causes an endless loop.
Return Value 

strtok returns a pointer to the token found in s1. A NULL pointer is returned when there are no more tokens. 

Example  

 #include <string.h>
 #include <stdio.h>
 int main(void)
 {
    char input[16] = "abc,d";
    char *p;
    /* strtok places a NULL terminator
    in front of the token, if found */
    p = strtok(input, ",");
    if (p)   printf("%s\n", p);
    /* A second call to strtok using a NULL
    as the first parameter returns a pointer
    to the character following the token  */
    p = strtok(NULL, ",");
    if (p)   printf("%s\n", p);
    return 0;
 }

Portability

 
POSIX 
Win32 
ANSI C 
ANSI C++ 
strtok 
_mbstok 
 
 
 
wcstok 
 
Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!