function
<cwchar>
wcstok
wchar_t* wcstok (wchar_t* wcs, const wchar_t* delimiters, wchar_t** p);
Split wide string into tokens
A sequence of calls to this function split wcs into tokens, which are sequences of contiguous wide characters separated by any of the wide characters that are part of delimiters.
On a first call, the function expects a C wide string as argument for wcs, whose first character is used as the starting location to scan for tokens. In subsequent calls, the function expects a null pointer and uses the position right after the end of last token as the new starting location for scanning.
This is the wide character equivalent of strtok (<cstdlib>), and operates in a similar way (see strtok for more details), but note that unlike strtok, this function takes an additional argument in which the state of the tokenization process is stored.
Parameters
- wcs
- C wide string to truncate.
Notice that the contents of this string are modified and broken into smaller strings (tokens).
Alternativelly, a null pointer may be specified, in which case the function continues scanning where a previous successful call to the function ended. This requires the third argument (p) to be the same as in that previous call.
- delimiters
- C wide string containing the delimiter wide characters.
These may vary from one call to another.
- p
- Pointer to a wchar_t pointer. The function uses this to store a value of type wchar_t* with the state of the current tokenization sequence. This same pointer shall be passed to the subsequent calls to this function to tokenize the rest of the wide string.
Return Value
A pointer to the last token found in the wide string.
A null pointer is returned if there are no tokens left to retrieve.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
/* wcstok example */
#include <wchar.h>
int main ()
{
wchar_t wcs[] = L"- This, a sample string.";
wchar_t * pwc;
wchar_t * pt;
wprintf (L"Splitting wide string \"%ls\" into tokens:\n",wcs);
pwc = wcstok (wcs, L" ,.-", &pt);
while (pwc != NULL)
{
wprintf (L"%ls\n",pwc);
pwc = wcstok (NULL, L" ,.-", &pt);
}
return 0;
}
|
Output:
Splitting wide string "- This, a sample string." into tokens:
This
a
sample
string
|
See also.
- strtok
- Split string into tokens (function)
- wcscspn
- Get span until character in wide string (function)
- wcspbrk
- Locate characters in wide string (function)