strtok_s, _strtok_s_l, wcstok_s, _wcstok_s_l, _mbstok_s, _mbstok_s_l

通过使用当前区域设置或传入的区域设置,查找字符串中的下一个标记。 这些版本的 strtok_strtok_lwcstok_wcstok_l_mbstok_mbstok_l 具有安全增强功能,如 CRT 中的安全功能中所述。

重要

_mbstok_s_mbstok_s_l 无法用于在 Windows 运行时中执行的应用程序。 有关详细信息,请参阅通用 Windows 平台应用中不支持的 CRT 函数

语法

char* strtok_s(
   char* str,
   const char* delimiters,
   char** context
);

char* _strtok_s_l(
   char* str,
   const char* delimiters,
   char** context,
   _locale_t locale
);

wchar_t* wcstok_s(
   wchar_t* str,
   const wchar_t* delimiters,
   wchar_t** context
);

wchar_t *_wcstok_s_l(
   wchar_t* str,
   const wchar_t* delimiters,
   wchar_t** context,
   _locale_t locale
);

unsigned char* _mbstok_s(
   unsigned char* str,
   const unsigned char* delimiters,
   char** context
);

unsigned char* _mbstok_s_l(
   unsigned char* str,
   const unsigned char* delimiters,
   char** context,
   _locale_t locale
);

参数

str
包含要查找的一个或多个令牌的字符串。

delimiters
要使用的分隔符字符集。

context
用于存储调用函数之间的位置信息。

locale
要使用的区域设置。

返回值

返回 str 中找到的指向下一个标记的指针。 在未找到更多标记时返回 NULL。 每次调用都会修改 str,方法是通过在标记返回后,为出现的第一个分隔符替换空字符。

错误条件

str delimiters context 返回值 errno
NULL any 指向空指针的指针 NULL EINVAL
any NULL any NULL EINVAL
任意 任意 NULL NULL EINVAL

如果 strNULL,但 context 是指向有效上下文指针的指针,则不会有任何错误。

备注

strtok_s 函数系列在 str 中查找下一个标记。 delimiters 中的字符组指定在当前调用上的 str 中找到的可能的标记分隔符。 wcstok_s_mbstok_s 分别是 strtok_s 的宽字符及多字节字符版本。 wcstok_s_wcstok_s_l 的自变量和返回值是宽字符字符串。 _mbstok_s_mbstok_s_l 的自变量和返回值是多字节字符字符串。 否则这些函数具有相同行为。

此函数验证其参数。 如果出现“错误条件”表中的错误条件,则会调用无效的参数处理程序,如参数验证中所述。 如果允许执行继续,则这些功能将 errno 设置为 EINVAL 并返回 NULL

首次调用 strtok_s 时,函数跳过前导分隔符并返回指向 str 中的第一个标记的指针,终止空字符的标记。 通过对 strtok_s 的一系列调用,可以从 str 的其余部分中分离出更多标记。 每次调用 strtok_s 都会修改 str,方法是在通过调用返回标记后插入空字符。 context 指针跟踪要读取的字符串以及在字符串中读取下一个标记的位置。 若要读取来自 str 的下一个标记,请使用 str 实参的 NULL 值调用 strtok_s并传递同一 context 形参。 NULLstr 参数使 strtok_s 在修改的 str 中搜索下一个标记。 delimiters 实参可以采用从第一个调用到下一个调用的任何值,以使分隔符集有所不同。

由于 context 参数取代了 strtok_strtok_l 中使用的静态缓冲区,因此,可以在同一线程中同时分析两个字符串。

输出值受区域设置的 LC_CTYPE 类别设置的影响。 有关详细信息,请参阅 setlocale

不带 _l 后缀的这些函数的版本会将当前区域设置用于此线程区域设置的相关行为。 带 _l 后缀的版本是相同的,只不过它们改为了使用 locale 参数指定的区域设置。 有关详细信息,请参阅 Locale

默认情况下,此函数的全局状态范围限定为应用程序。 若要更改此行为,请参阅 CRT 中的全局状态

一般文本例程映射

TCHAR.H 例程 _UNICODE_MBCS 未定义 _MBCS 已定义 _UNICODE 已定义
_tcstok_s strtok_s _mbstok_s wcstok_s
_tcstok_s_l _strtok_s_l _mbstok_s_l _wcstok_s_l

要求

例程 必需的标头
strtok_s <string.h>
_strtok_s_l <string.h>
wcstok_s
_wcstok_s_l
<string.h><wchar.h>
_mbstok_s
_mbstok_s_l
<mbstring.h>

有关兼容性的详细信息,请参阅 兼容性

示例

// crt_strtok_s.c
// In this program, a loop uses strtok_s
// to print all the tokens (separated by commas
// or blanks) in two strings at the same time.

#include <string.h>
#include <stdio.h>

char string1[] =
    "A string\tof ,,tokens\nand some  more tokens";
char string2[] =
    "Another string\n\tparsed at the same time.";
char seps[]   = " ,\t\n";
char *token1 = NULL;
char *token2 = NULL;
char *next_token1 = NULL;
char *next_token2 = NULL;

int main(void)
{
    printf("Tokens:\n");

    // Establish string and get the first token:
    token1 = strtok_s(string1, seps, &next_token1);
    token2 = strtok_s(string2, seps, &next_token2);

    // While there are tokens in "string1" or "string2"
    while ((token1 != NULL) || (token2 != NULL))
    {
        // Get next token:
        if (token1 != NULL)
        {
            printf(" %s\n", token1);
            token1 = strtok_s(NULL, seps, &next_token1);
        }
        if (token2 != NULL)
        {
            printf("        %s\n", token2);
            token2 = strtok_s(NULL, seps, &next_token2);
        }
    }
}
Tokens:
A
        Another
string
        string
of
        parsed
tokens
        at
and
        the
some
        same
more
        time.
tokens

另请参阅

字符串操作
区域设置
多字节字符序列的解释
strcspnwcscspn_mbscspn_mbscspn_l
strspnwcsspn_mbsspn_mbsspn_l