strtok, _strtok_l, wcstok, _wcstok_l, _mbstok, _mbstok_l
Find the next token in a string, using the current locale or a specified locale passed in. More secure versions of these functions are available; see strtok_s, _strtok_s_l, wcstok_s, _wcstok_s_l, _mbstok_s, _mbstok_s_l.
char *strtok(
char *strToken,
const char *strDelimit
);
wchar_t *wcstok(
wchar_t *strToken,
const wchar_t *strDelimit
);
unsigned char *_mbstok(
unsigned char*strToken,
const unsigned char *strDelimit
);
unsigned char *_mbstok(
unsigned char*strToken,
const unsigned char *strDelimit,
_locale_t locale
);
Параметры
strToken
String containing token or tokens.strDelimit
Set of delimiter characters.locale
Locale to use.
Возвращаемое значение
Returns a pointer to the next token found in strToken. They return NULL when no more tokens are found. Each call modifies strToken by substituting a NULL character for each delimiter that is encountered.
Заметки
The strtok function finds the next token in strToken. The set of characters in strDelimit specifies possible delimiters of the token to be found in strToken on the current call. wcstok and _mbstok are wide-character and multibyte-character versions of strtok. The arguments and return value of wcstok are wide-character strings; those of _mbstok are multibyte-character strings. These three functions behave identically otherwise.
Примечание о безопасности. |
---|
These functions incur a potential threat brought about by a buffer overrun problem. Buffer overrun problems are a frequent method of system attack, resulting in an unwarranted elevation of privilege. For more information, see Avoiding Buffer Overruns. |
On the first call to strtok, the function skips leading delimiters and returns a pointer to the first token in strToken, terminating the token with a null character. More tokens can be broken out of the remainder of strToken by a series of calls to strtok. Each call to strtokmodifies strToken by inserting a null character after the token returned by that call. To read the next token from strToken, call strtok with a NULL value for the strToken argument. The NULL strToken argument causes strtok to search for the next token in the modified strToken. The strDelimit argument can take any value from one call to the next so that the set of delimiters may vary.
The output value is affected by the setting of the LC_CTYPE category setting of the locale; see setlocale for more information. The versions of these functions without the _l suffix use the current locale for this locale-dependent behavior; the versions with the _l suffix are identical except that they use the locale parameter passed in instead. For more information, see Locale.
Примечание. |
---|
Each function uses a static variable for parsing the string into tokens. If multiple or simultaneous calls are made to the same function, a high potential for data corruption and inaccurate results exists. Therefore, do not attempt to call the same function simultaneously for different strings and be aware of calling one of these functions from within a loop where another routine may be called that uses the same function. However, calling this function simultaneously from multiple threads does not have undesirable effects. |
Generic-Text Routine Mappings
TCHAR.H routine |
_UNICODE & _MBCS not defined |
_MBCS defined |
_UNICODE defined |
---|---|---|---|
_tcstok |
strtok |
_mbstok |
wcstok |
_tcstok |
_strtok_l |
_mbstok_l |
_wcstok_l |
Требования
Routine |
Required header |
---|---|
strtok |
<string.h> |
wcstok |
<string.h> or <wchar.h> |
_mbstok, _mbstok_l |
<mbstring.h> |
For additional compatibility information, see Compatibility in the Introduction.
Пример
// crt_strtok.c
// compile with: /W3
// In this program, a loop uses strtok
// to print all the tokens (separated by commas
// or blanks) in the string named "string".
//
#include <string.h>
#include <stdio.h>
char string[] = "A string\tof ,,tokens\nand some more tokens";
char seps[] = " ,\t\n";
char *token;
int main( void )
{
printf( "Tokens:\n" );
// Establish string and get the first token:
token = strtok( string, seps ); // C4996
// Note: strtok is deprecated; consider using strtok_s instead
while( token != NULL )
{
// While there are tokens in "string"
printf( " %s\n", token );
// Get next token:
token = strtok( NULL, seps ); // C4996
}
}
Tokens:
A
string
of
tokens
and
some
more
tokens
Эквивалент в .NET Framework
Not applicable. To call the standard C function, use PInvoke. For more information, see Platform Invoke Examples.
См. также
Основные понятия
Interpretation of Multibyte-Character Sequences